Class: EacFs::StorageTree
Constant Summary collapse
- CONTENT_FILE_NAME =
'__content__'
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #child(*child_path_parts) ⇒ Object
- #clear ⇒ Object
- #content_path ⇒ Object
-
#initialize(*path_parts) ⇒ StorageTree
constructor
A new instance of StorageTree.
- #read ⇒ Object
- #read_or_store ⇒ Object
- #read_or_store_yaml(use_cache = true) ⇒ Object
- #read_yaml ⇒ Object?
- #stored? ⇒ Boolean
- #write(value) ⇒ Object
- #write_yaml(object) ⇒ Object
Constructor Details
#initialize(*path_parts) ⇒ StorageTree
Returns a new instance of StorageTree.
13 14 15 16 17 |
# File 'lib/eac_fs/storage_tree.rb', line 13 def initialize(*path_parts) raise ArgumentError, "\"#{path_parts}\" is empty" if path_parts.empty? @path = ::File.(::File.join(*path_parts.map(&:to_s))) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/eac_fs/storage_tree.rb', line 11 def path @path end |
Instance Method Details
#child(*child_path_parts) ⇒ Object
61 62 63 |
# File 'lib/eac_fs/storage_tree.rb', line 61 def child(*child_path_parts) self.class.new(path, *child_path_parts) end |
#clear ⇒ Object
19 20 21 22 23 |
# File 'lib/eac_fs/storage_tree.rb', line 19 def clear return unless stored? ::File.unlink(content_path) end |
#content_path ⇒ Object
69 70 71 |
# File 'lib/eac_fs/storage_tree.rb', line 69 def content_path ::File.join(path, CONTENT_FILE_NAME) end |
#read ⇒ Object
25 26 27 28 29 |
# File 'lib/eac_fs/storage_tree.rb', line 25 def read return nil unless stored? ::File.read(content_path) end |
#read_or_store ⇒ Object
31 32 33 34 35 |
# File 'lib/eac_fs/storage_tree.rb', line 31 def read_or_store write(yield) unless stored? read end |
#read_or_store_yaml(use_cache = true) ⇒ Object
38 39 40 41 42 |
# File 'lib/eac_fs/storage_tree.rb', line 38 def read_or_store_yaml(use_cache = true) # rubocop:disable Style/OptionalBooleanParameter write_yaml(yield) unless stored? && use_cache read_yaml end |
#read_yaml ⇒ Object?
45 46 47 48 |
# File 'lib/eac_fs/storage_tree.rb', line 45 def read_yaml r = read r.nil? ? nil : ::EacRubyUtils::Yaml.load(r) end |
#stored? ⇒ Boolean
65 66 67 |
# File 'lib/eac_fs/storage_tree.rb', line 65 def stored? ::File.exist?(content_path) end |
#write(value) ⇒ Object
50 51 52 53 54 |
# File 'lib/eac_fs/storage_tree.rb', line 50 def write(value) assert_directory_on_path ::File.write(content_path, value) value end |
#write_yaml(object) ⇒ Object
56 57 58 59 |
# File 'lib/eac_fs/storage_tree.rb', line 56 def write_yaml(object) write(::EacRubyUtils::Yaml.dump(object)) object end |