Class: EacFs::StorageTree

Inherits:
Object show all
Defined in:
lib/eac_fs/storage_tree.rb

Constant Summary collapse

CONTENT_FILE_NAME =
'__content__'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*path_parts) ⇒ StorageTree

Returns a new instance of StorageTree.

Raises:

  • (ArgumentError)


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.expand_path(::File.join(*path_parts.map(&:to_s)))
end

Instance Attribute Details

#pathObject (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

#clearObject



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_pathObject



69
70
71
# File 'lib/eac_fs/storage_tree.rb', line 69

def content_path
  ::File.join(path, CONTENT_FILE_NAME)
end

#readObject



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_storeObject



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

Returns:



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_yamlObject?

Returns:



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

Returns:

  • (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