Class: FileStructure
- Inherits:
-
Object
- Object
- FileStructure
- 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
-
#mountpoint ⇒ String?
readonly
The current mountpoint.
-
#structure ⇒ Hash
readonly
The file structure definition.
Class Method Summary collapse
-
.build(&block) ⇒ FileStructure
Build a new FileStructure using the DSL.
Instance Method Summary collapse
-
#initialize(structure) ⇒ FileStructure
constructor
A new instance of FileStructure.
-
#mount(dirname) ⇒ Object
Effectively creates files and directories in the specified directory.
-
#mounted? ⇒ Boolean
Check if the file structure is currently mounted.
-
#path_for(target) ⇒ String?
Get the absolute path for a file in the mounted file structure.
-
#unmount ⇒ Object
Remove all files from the mountpoint.
Methods included from Contract
Constructor Details
#initialize(structure) ⇒ FileStructure
Returns a new instance of FileStructure.
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
#mountpoint ⇒ String? (readonly)
Returns the current mountpoint.
16 17 18 |
# File 'lib/file_structure.rb', line 16 def mountpoint @mountpoint end |
#structure ⇒ Hash (readonly)
Returns 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.
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.
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.
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.
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 |
#unmount ⇒ Object
Remove all files from the mountpoint.
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 |