Class: Uniword::Resource::CacheVersion
- Inherits:
-
Object
- Object
- Uniword::Resource::CacheVersion
- Defined in:
- lib/uniword/resource/cache_version.rb
Overview
MODEL for cache version tracking Has state (cache reference) and behavior (check staleness, update)
Does NOT extend Lutaml::Model::Serializable.
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
Instance Method Summary collapse
-
#cached_version ⇒ Object
Get cached Word version.
-
#delete ⇒ Object
Delete version file.
-
#exists? ⇒ Boolean
Check if cache exists.
-
#initialize(cache) ⇒ CacheVersion
constructor
A new instance of CacheVersion.
-
#stale?(current_word_version) ⇒ Boolean
Check if cache is stale (Word was updated).
-
#update(word_version) ⇒ Object
Update cached version.
Constructor Details
#initialize(cache) ⇒ CacheVersion
Returns a new instance of CacheVersion.
14 15 16 |
# File 'lib/uniword/resource/cache_version.rb', line 14 def initialize(cache) @cache = cache end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
12 13 14 |
# File 'lib/uniword/resource/cache_version.rb', line 12 def cache @cache end |
Instance Method Details
#cached_version ⇒ Object
Get cached Word version
19 20 21 22 23 24 25 |
# File 'lib/uniword/resource/cache_version.rb', line 19 def cached_version return nil unless File.exist?(cache.paths.version_file) JSON.parse(File.read(cache.paths.version_file))["word_version"] rescue JSON::ParserError nil end |
#delete ⇒ Object
Delete version file
45 46 47 |
# File 'lib/uniword/resource/cache_version.rb', line 45 def delete FileUtils.rm_f(cache.paths.version_file) end |
#exists? ⇒ Boolean
Check if cache exists
40 41 42 |
# File 'lib/uniword/resource/cache_version.rb', line 40 def exists? File.exist?(cache.paths.version_file) end |
#stale?(current_word_version) ⇒ Boolean
Check if cache is stale (Word was updated)
35 36 37 |
# File 'lib/uniword/resource/cache_version.rb', line 35 def stale?(current_word_version) cached_version != current_word_version end |
#update(word_version) ⇒ Object
Update cached version
28 29 30 31 32 |
# File 'lib/uniword/resource/cache_version.rb', line 28 def update(word_version) cache.paths.ensure_directories_exist! File.write(cache.paths.version_file, JSON.generate(word_version: word_version)) end |