Class: EacRubyUtils::FilesystemCache
- Defined in:
- lib/eac_ruby_utils/filesystem_cache.rb
Constant Summary collapse
- CONTENT_FILE_NAME =
'__content__'
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #cached? ⇒ Boolean
- #child(*child_path_parts) ⇒ Object
- #clear ⇒ Object
- #content_path ⇒ Object
-
#initialize(*path_parts) ⇒ FilesystemCache
constructor
A new instance of FilesystemCache.
- #read ⇒ Object
- #read_or_cache ⇒ Object
- #write(value) ⇒ Object
Constructor Details
#initialize(*path_parts) ⇒ FilesystemCache
Returns a new instance of FilesystemCache.
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.(::File.join(*path_parts.map(&:to_s))) end |
Instance Attribute Details
#path ⇒ Object (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
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 |
#clear ⇒ Object
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_path ⇒ Object
47 48 49 |
# File 'lib/eac_ruby_utils/filesystem_cache.rb', line 47 def content_path ::File.join(path, CONTENT_FILE_NAME) end |
#read ⇒ Object
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_cache ⇒ Object
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 |