Class: Uniword::Resource::CacheVersion

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#cacheObject (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_versionObject

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

#deleteObject

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

Returns:

  • (Boolean)


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)

Returns:

  • (Boolean)


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