Class: FileStructure
- Inherits:
-
Object
- Object
- FileStructure
- 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
Defined Under Namespace
Modules: Contract Classes: Validator
Constant Summary collapse
- VERSION =
'0.1.0'
Instance Attribute Summary collapse
-
#mountpoint ⇒ Object
readonly
Returns the value of attribute mountpoint.
-
#structure ⇒ Object
readonly
Returns the value of attribute structure.
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
-
#path_for(*args) ⇒ String
Get the full path for a file in the mounted file structure.
-
#unmount ⇒ Object
Remove all files from the mountpoint.
Constructor Details
#initialize(structure) ⇒ FileStructure
Returns a new instance of FileStructure.
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
#mountpoint ⇒ Object (readonly)
Returns the value of attribute mountpoint.
19 20 21 |
# File 'lib/file_structure.rb', line 19 def mountpoint @mountpoint end |
#structure ⇒ Object (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.
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
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.
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 |
#unmount ⇒ Object
Remove all files from the mountpoint.
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 |