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)


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

Instance Attribute Details

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

#clearObject



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_pathObject



67
68
69
# File 'lib/eac_fs/storage_tree.rb', line 67

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

#readObject



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_storeObject



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

Returns:



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

Returns:



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

Returns:

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