Class: FileStructure

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

Overview

Manage files, directories and links described with a file structure definition as a Ruby Hash. A valid file structure definition is an Array of Hashes with the following possible file definitions:

rubocop:disable Layout/LineLength rubocop:enable Layout/LineLength

Examples:

{ name: 'myfile', type: :file, content: 'abc', ref: 'fileref'} # :content and :ref are optional
{ name: 'mydir' type: :directory, children: [<another valid file structure definition>], ref: 'dirref' } # :ref is optional
{ name: 'mylink', type: :link, to: 'fileref' } # link to 'myfile' refereced earlier by 'fileref'

Defined Under Namespace

Modules: Contract Classes: Validator

Constant Summary collapse

VERSION =
'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(structure) ⇒ FileStructure

Returns a new instance of FileStructure.

Parameters:

  • structure (Array<Hash>)

    a valid file structure definition.

Raises:

  • (AssertionError)

    if the file structure is invalid



23
24
25
26
27
28
# File 'lib/file_structure.rb', line 23

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

  @structure = structure
  @mountpoint = nil
end

Instance Attribute Details

#mountpointObject (readonly)

Returns the value of attribute mountpoint.



19
20
21
# File 'lib/file_structure.rb', line 19

def mountpoint
  @mountpoint
end

#structureObject (readonly)

Returns the value of attribute structure.



19
20
21
# File 'lib/file_structure.rb', line 19

def structure
  @structure
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:

  • (AssertionError)

    if the FileStructure is already mounted

See Also:



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/file_structure.rb', line 36

def mount(dirname)
  Contract.assert(!mounted?, 'file structure is already mounted')

  mountpoint = File.absolute_path(dirname)
  FileUtils.mkdir_p(mountpoint) unless Dir.exist?(mountpoint)
  begin
    create_file_structure(mountpoint, @structure)
  rescue StandardErrror => e
    FileUtils.rm_r(Dir.glob("#{mountpoint}/*")) # clear residuals
    raise e
  end
  @mountpoint = mountpoint
end

#mounted?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/file_structure.rb', line 61

def mounted?
  !!@mountpoint
end

#path_for(*args) ⇒ String

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

Examples:

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

Parameters:

  • args (Symbol, String Array<Symbol, String>)

    the recursive names to the desired file or directory

Returns:

  • (String)

    the full path to the specified file/directory

Raises:

  • (AssertionError)

    if the file structure is not mounted



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

def path_for(*args)
  Contract.assert(mounted?, 'file structure is not mounted')

  finder = [*args].flatten.map(&:to_sym)
  build_path(finder, @structure)
end

#unmountObject

Remove all files from the mountpoint.

Returns:

  • void

See Also:



54
55
56
57
58
59
# File 'lib/file_structure.rb', line 54

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

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