class EacRubyUtils::FilesystemCache

Constants

CONTENT_FILE_NAME

Attributes

path[R]

Public Class Methods

new(*path_parts) click to toggle source
# 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

Public Instance Methods

cached?() click to toggle source
# File lib/eac_ruby_utils/filesystem_cache.rb, line 43
def cached?
  ::File.exist?(content_path)
end
child(*child_path_parts) click to toggle source
# File lib/eac_ruby_utils/filesystem_cache.rb, line 39
def child(*child_path_parts)
  self.class.new(path, *child_path_parts)
end
clear() click to toggle source
# File lib/eac_ruby_utils/filesystem_cache.rb, line 15
def clear
  return unless cached?

  ::File.unlink(content_path)
end
content_path() click to toggle source
# File lib/eac_ruby_utils/filesystem_cache.rb, line 47
def content_path
  ::File.join(path, CONTENT_FILE_NAME)
end
read() click to toggle source
# File lib/eac_ruby_utils/filesystem_cache.rb, line 21
def read
  return nil unless cached?

  ::File.read(content_path)
end
read_or_cache() { || ... } click to toggle source
# File lib/eac_ruby_utils/filesystem_cache.rb, line 27
def read_or_cache
  write(yield) unless cached?

  read
end
write(value) click to toggle source
# File lib/eac_ruby_utils/filesystem_cache.rb, line 33
def write(value)
  assert_directory_on_path
  ::File.write(content_path, value)
  value
end

Private Instance Methods

assert_directory_on_path() click to toggle source
# File lib/eac_ruby_utils/filesystem_cache.rb, line 53
def assert_directory_on_path
  raise "#{path} is a file" if ::File.file?(path)

  ::FileUtils.mkdir_p(path)
end