Class: EacRubyUtils::FilesystemCache

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_ruby_utils/filesystem_cache.rb

Constant Summary collapse

CONTENT_FILE_NAME =
'__content__'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*path_parts) ⇒ FilesystemCache

Returns a new instance of FilesystemCache.

Raises:

  • (ArgumentError)

9
10
11
12
13
# File 'lib/eac_ruby_utils/filesystem_cache.rb', line 9

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


7
8
9
# File 'lib/eac_ruby_utils/filesystem_cache.rb', line 7

def path
  @path
end

Instance Method Details

#cached?Boolean

Returns:


37
38
39
# File 'lib/eac_ruby_utils/filesystem_cache.rb', line 37

def cached?
  ::File.exist?(content_path)
end

#child(*child_path_parts) ⇒ Object


33
34
35
# File 'lib/eac_ruby_utils/filesystem_cache.rb', line 33

def child(*child_path_parts)
  self.class.new(path, *child_path_parts)
end

#clearObject


15
16
17
18
19
# File 'lib/eac_ruby_utils/filesystem_cache.rb', line 15

def clear
  return unless cached?

  ::File.unlink(content_path)
end

#content_pathObject


41
42
43
# File 'lib/eac_ruby_utils/filesystem_cache.rb', line 41

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

#readObject


21
22
23
24
25
# File 'lib/eac_ruby_utils/filesystem_cache.rb', line 21

def read
  return nil unless cached?

  ::File.read(content_path)
end

#write(value) ⇒ Object


27
28
29
30
31
# File 'lib/eac_ruby_utils/filesystem_cache.rb', line 27

def write(value)
  assert_directory_on_path
  ::File.write(content_path, value)
  value
end