Class: FileStructure::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/file_structure/dsl.rb

Overview

Provide a DSL to easily create file structure definitions.

Examples:

structure = FileStructure::DSL.eval do
  directory 'dir_a' do
    file 'file_a'
    symlink 'point_to_file_c', to: 'file_c_ref'
    directory 'dir_b' do
      file 'file_b'
      file 'file_c', ref: 'file_c_ref'
    end
  end
end
# => [{ type: :directory, name: 'dir_a', children: [ ... ] }]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSL

Returns a new instance of DSL.



31
32
33
# File 'lib/file_structure/dsl.rb', line 31

def initialize
  @structure = []
end

Instance Attribute Details

#structureArray (readonly)

Returns the resulting file structure definition.

Returns:

  • (Array)

    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.

See Also:



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.

Parameters:

  • name (String)

    the name of the directory

  • ref (String) (defaults to: name)

    the reference to use for symlinks

Returns:

  • (Hash)

    the created directory definition



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.

Parameters:

  • name (String)

    the name of the file

  • content (String) (defaults to: nil)

    the content of the file

  • ref (String) (defaults to: name)

    the reference to use for symlinks

Returns:

  • (Hash)

    the created file definition



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

Add a symlink to the parent structure.

Parameters:

  • name (String)

    the name of the symlink

  • to (String)

    the reference of the file to point to

Returns:

  • (Hash)

    the created symlink definition



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