Class: FileStructure::DSL
- Inherits:
-
Object
- Object
- FileStructure::DSL
- Defined in:
- lib/file_structure/dsl.rb
Overview
Provide a DSL to easily create file structure definitions.
Instance Attribute Summary collapse
-
#structure ⇒ Array
readonly
The resulting file structure definition.
Class Method Summary collapse
-
.eval(&block) ⇒ Object
Return the file structure definition builded with the given block.
Instance Method Summary collapse
-
#directory(name, ref: name, &block) ⇒ Hash
Add a directory to the parent structure.
-
#file(name, content: nil, ref: name) ⇒ Hash
Add a file definition to the parent structure.
-
#initialize ⇒ DSL
constructor
A new instance of DSL.
-
#symlink(name, to:) ⇒ Hash
Add a symlink to the parent structure.
Constructor Details
#initialize ⇒ DSL
Returns a new instance of DSL.
31 32 33 |
# File 'lib/file_structure/dsl.rb', line 31 def initialize @structure = [] end |
Instance Attribute Details
#structure ⇒ Array (readonly)
Returns the resulting file structure definition.
20 21 22 |
# File 'lib/file_structure/dsl.rb', line 20 def structure @structure end |
Class Method Details
.eval(&block) ⇒ Object
Return the file structure definition builded with the given block.
25 26 27 28 29 |
# File 'lib/file_structure/dsl.rb', line 25 def self.eval(&block) dsl = new dsl.instance_eval(&block) dsl.structure end |
Instance Method Details
#directory(name, ref: name, &block) ⇒ Hash
Add a directory to the parent structure.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/file_structure/dsl.rb', line 63 def directory(name, ref: name, &block) dsl = self.class.new dsl.instance_eval(&block) children = dsl.structure directory = { type: :directory, name: name, children: children, ref: ref } @structure << directory and return directory end |
#file(name, content: nil, ref: name) ⇒ Hash
Add a file definition to the parent structure.
41 42 43 44 45 46 |
# File 'lib/file_structure/dsl.rb', line 41 def file(name, content: nil, ref: name) file = { type: :file, name: name } file[:content] = content if content file[:ref] = ref @structure << file and return file end |
#symlink(name, to:) ⇒ Hash
Add a symlink to the parent structure.
53 54 55 56 |
# File 'lib/file_structure/dsl.rb', line 53 def symlink(name, to:) symlink = { type: :symlink, name: name, to: to } @structure << symlink and return symlink end |