Class: Uniword::Resource::Cache
- Inherits:
-
Object
- Object
- Uniword::Resource::Cache
- Defined in:
- lib/uniword/resource/cache.rb
Overview
MODEL for cache operations Has state (paths, cached resources) and behavior (CRUD)
Does NOT extend Lutaml::Model::Serializable because it doesn’t need serialization.
Instance Attribute Summary collapse
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
Instance Method Summary collapse
-
#clear!(type: :all) ⇒ Object
Clear all cache or specific type.
-
#count(type) ⇒ Object
Count cached resources of a type.
-
#delete(type, name) ⇒ Object
Delete resource from cache.
-
#exists?(type, name) ⇒ Boolean
Check if resource exists in cache.
-
#initialize(word_impl = nil) ⇒ Cache
constructor
A new instance of Cache.
-
#list(type) ⇒ Object
List all cached resources of a type.
-
#read(type, name) ⇒ Object
Read resource from cache.
-
#write(type, name, content) ⇒ Object
Write resource to cache.
Constructor Details
#initialize(word_impl = nil) ⇒ Cache
Returns a new instance of Cache.
12 13 14 15 |
# File 'lib/uniword/resource/cache.rb', line 12 def initialize(word_impl = nil) word_impl ||= Uniword::WordImplementation.detect @paths = CachePaths.from_word_implementation(word_impl) end |
Instance Attribute Details
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
10 11 12 |
# File 'lib/uniword/resource/cache.rb', line 10 def paths @paths end |
Instance Method Details
#clear!(type: :all) ⇒ Object
Clear all cache or specific type
56 57 58 59 60 61 62 63 |
# File 'lib/uniword/resource/cache.rb', line 56 def clear!(type: :all) if type == :all FileUtils.rm_rf(paths.base) else dir = directory_for_type(type) FileUtils.rm_rf(dir) end end |
#count(type) ⇒ Object
Count cached resources of a type
45 46 47 |
# File 'lib/uniword/resource/cache.rb', line 45 def count(type) list(type).size end |
#delete(type, name) ⇒ Object
Delete resource from cache
50 51 52 53 |
# File 'lib/uniword/resource/cache.rb', line 50 def delete(type, name) path = resource_path(type, name) FileUtils.rm_f(path) end |
#exists?(type, name) ⇒ Boolean
Check if resource exists in cache
32 33 34 |
# File 'lib/uniword/resource/cache.rb', line 32 def exists?(type, name) File.exist?(resource_path(type, name)) end |
#list(type) ⇒ Object
List all cached resources of a type
37 38 39 40 41 42 |
# File 'lib/uniword/resource/cache.rb', line 37 def list(type) dir = directory_for_type(type) return [] unless File.directory?(dir) Dir.glob(File.join(dir, "*.yml")).map { |f| File.basename(f, ".yml") } end |
#read(type, name) ⇒ Object
Read resource from cache
26 27 28 29 |
# File 'lib/uniword/resource/cache.rb', line 26 def read(type, name) path = resource_path(type, name) File.read(path) if File.exist?(path) end |
#write(type, name, content) ⇒ Object
Write resource to cache
18 19 20 21 22 23 |
# File 'lib/uniword/resource/cache.rb', line 18 def write(type, name, content) paths.ensure_directories_exist! path = resource_path(type, name) File.write(path, content) path end |