Class: FileStructure

Inherits:
Object
  • Object
show all
Includes:
Contract
Defined in:
lib/file_structure.rb,
lib/file_structure/dsl.rb,
lib/file_structure/version.rb,
lib/file_structure/contract.rb,
lib/file_structure/validator.rb

Defined Under Namespace

Modules: Contract Classes: DSL

Constant Summary collapse

VERSION =
'0.3.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Contract

assert

Constructor Details

#initialize(structure) ⇒ FileStructure

Returns a new instance of FileStructure.

Parameters:

  • structure (Array<Hash>)

    a valid file structure definition (see DSL)

Raises:



35
36
37
38
39
40
# File 'lib/file_structure.rb', line 35

def initialize(structure)
  assert(valid_file_structure?(structure), 'invalid file structure')

  @structure = structure
  @mountpoint = nil
end

Instance Attribute Details

#mountpointString? (readonly)

Returns the current mountpoint.

Returns:

  • (String, nil)

    the current mountpoint



16
17
18
# File 'lib/file_structure.rb', line 16

def mountpoint
  @mountpoint
end

#structureHash (readonly)

Returns the file structure definition.

Returns:

  • (Hash)

    the file structure definition



13
14
15
# File 'lib/file_structure.rb', line 13

def structure
  @structure
end

Class Method Details

.build(&block) ⇒ FileStructure

Build a new FileStructure using the DSL.

Examples:

FileStructure.build do
  dir 'foo' do
    file 'bar'
  end
end

Returns:

See Also:



29
30
31
# File 'lib/file_structure.rb', line 29

def self.build(&block)
  new(FileStructure::DSL.eval(&block))
end

Instance Method Details

#mount(dirname) ⇒ Object

Effectively creates files and directories in the specified directory.

Parameters:

  • dirname (String)

    the target directory

Returns:

  • void

Raises:

See Also:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/file_structure.rb', line 49

def mount(dirname)
  assert(!mounted?, 'file structure is already mounted')
  if Dir.exist?(dirname)
    assert(Dir.empty?(dirname), 'target directory is not empty')
  end

  mountpoint = File.absolute_path(dirname)
  FileUtils.mkdir_p(mountpoint) unless Dir.exist?(mountpoint)
  create_file_structure(mountpoint, @structure)
  @mountpoint = mountpoint
end

#mounted?Boolean

Check if the file structure is currently mounted.

Returns:

  • (Boolean)


76
77
78
# File 'lib/file_structure.rb', line 76

def mounted?
  !!@mountpoint
end

#path_for(target) ⇒ String?

Get the absolute path for a file in the mounted file structure.

Examples:

path_for('foo/bar/file')
# => "/path/to/mountpoint/foo/bar/file"

Parameters:

  • target (String)

    the relative path of the target in the file structure

Returns:

  • (String)

    the full path to the target

  • (nil)

    if no target has been found

Raises:



90
91
92
93
94
95
# File 'lib/file_structure.rb', line 90

def path_for(target)
  assert(mounted?, 'file structure is not mounted')

  absolute_path = File.join(@mountpoint, target)
  File.exist?(absolute_path) ? absolute_path : nil
end

#unmountObject

Remove all files from the mountpoint.

Returns:

  • void

Raises:

See Also:



66
67
68
69
70
71
# File 'lib/file_structure.rb', line 66

def unmount
  assert(mounted?, 'file structure is not mounted')

  FileUtils.rm_r(Dir.glob("#{@mountpoint}/*"))
  @mountpoint = nil
end