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:



43
44
45
# File 'lib/eac_ruby_utils/filesystem_cache.rb', line 43

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

#child(*child_path_parts) ⇒ Object



39
40
41
# File 'lib/eac_ruby_utils/filesystem_cache.rb', line 39

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



47
48
49
# File 'lib/eac_ruby_utils/filesystem_cache.rb', line 47

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

#read_or_cacheObject



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

def read_or_cache
  write(yield) unless cached?

  read
end

#write(value) ⇒ Object



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

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