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.
11 12 13 14 15 |
# File 'lib/eac_fs/storage_tree.rb', line 11 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.
9 10 11 |
# File 'lib/eac_fs/storage_tree.rb', line 9 def path @path end |
Instance Method Details
#child(*child_path_parts) ⇒ Object
59 60 61 |
# File 'lib/eac_fs/storage_tree.rb', line 59 def child(*child_path_parts) self.class.new(path, *child_path_parts) end |
#clear ⇒ Object
17 18 19 20 21 |
# File 'lib/eac_fs/storage_tree.rb', line 17 def clear return unless stored? ::File.unlink(content_path) end |
#content_path ⇒ Object
67 68 69 |
# File 'lib/eac_fs/storage_tree.rb', line 67 def content_path ::File.join(path, CONTENT_FILE_NAME) end |
#read ⇒ Object
23 24 25 26 27 |
# File 'lib/eac_fs/storage_tree.rb', line 23 def read return nil unless stored? ::File.read(content_path) end |
#read_or_store ⇒ Object
29 30 31 32 33 |
# File 'lib/eac_fs/storage_tree.rb', line 29 def read_or_store write(yield) unless stored? read end |
#read_or_store_yaml(use_cache = true) ⇒ Object
36 37 38 39 40 |
# File 'lib/eac_fs/storage_tree.rb', line 36 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?
43 44 45 46 |
# File 'lib/eac_fs/storage_tree.rb', line 43 def read_yaml r = read r.nil? ? nil : ::EacRubyUtils::Yaml.load(r) end |
#stored? ⇒ Boolean
63 64 65 |
# File 'lib/eac_fs/storage_tree.rb', line 63 def stored? ::File.exist?(content_path) end |
#write(value) ⇒ Object
48 49 50 51 52 |
# File 'lib/eac_fs/storage_tree.rb', line 48 def write(value) assert_directory_on_path ::File.write(content_path, value) value end |
#write_yaml(object) ⇒ Object
54 55 56 57 |
# File 'lib/eac_fs/storage_tree.rb', line 54 def write_yaml(object) write(::EacRubyUtils::Yaml.dump(object)) object end |