Class: UnitMeasurements::Cache
- Inherits:
-
Object
- Object
- UnitMeasurements::Cache
- Defined in:
- lib/unit_measurements/cache.rb
Overview
The UnitMeasurements::Cache
class manages caching of conversion factors between different units within a unit group. It provides methods to retrieve, set, and clear cached conversion factors.
Cached conversion factors are stored in JSON file on the file system.
Constant Summary collapse
- CACHE_DIRECTORY =
The directory path where cache files are stored.
File.(File.join("..", "..", "cache"), __dir__).freeze
Instance Attribute Summary collapse
-
#cached_data ⇒ Hash
readonly
Stores cached conversion factors between different units within a unit group.
Instance Method Summary collapse
-
#build_cache_file_path(unit_group) ⇒ String
private
Builds and returns an absolute path of the cache file.
-
#clear_cache ⇒ Object
Clears the entire cache.
-
#ensure_cache_directory_exists ⇒ Object
private
Ensures that the cache directory exists.
-
#get(source_unit, target_unit) ⇒ Numeric|NilClass
Retrieves the conversion factor between source and target units from the cache.
-
#initialize(unit_group) ⇒ Cache
constructor
Initializes a new
Cache
instance for a specific unit group. -
#load_cache ⇒ Hash
private
Loads the cache from the cache file.
-
#set(source_unit, target_unit, conversion_factor) ⇒ Object
Sets the conversion factor between source and target units in the cache.
-
#store_cache ⇒ Object
private
Stores the current cache data to the cache file.
Constructor Details
#initialize(unit_group) ⇒ Cache
Initializes a new Cache
instance for a specific unit group.
Initialization first ensures existence of the cache directory. If the cache directory does not exist, it gets created.
39 40 41 42 43 |
# File 'lib/unit_measurements/cache.rb', line 39 def initialize(unit_group) ensure_cache_directory_exists @cache_file = build_cache_file_path(unit_group) @cached_data ||= load_cache end |
Instance Attribute Details
#cached_data ⇒ Hash (readonly)
Stores cached conversion factors between different units within a unit group.
28 29 30 |
# File 'lib/unit_measurements/cache.rb', line 28 def cached_data @cached_data end |
Instance Method Details
#build_cache_file_path(unit_group) ⇒ String (private)
Builds and returns an absolute path of the cache file.
This method first checks if the cache file name is specified for the unit group. If yes, it builds absolute path of the cache file using specified cache file name. If not, it builds file name from of the name of the unit group. This file name is then used to build absolute path of the cache file.
166 167 168 169 170 171 |
# File 'lib/unit_measurements/cache.rb', line 166 def build_cache_file_path(unit_group) cache_file_name = unit_group.cache_file || unit_group.to_s.split("::").last.underscore cache_file_name = File.basename(cache_file_name, ".json") + ".json" Pathname.new(File.join(CACHE_DIRECTORY, cache_file_name)).cleanpath end |
#clear_cache ⇒ Object
Clears the entire cache.
79 80 81 82 |
# File 'lib/unit_measurements/cache.rb', line 79 def clear_cache @cached_data = {} store_cache end |
#ensure_cache_directory_exists ⇒ Object (private)
Ensures that the cache directory exists. If the cache directory does not exist, it gets created.
144 145 146 147 148 149 150 |
# File 'lib/unit_measurements/cache.rb', line 144 def ensure_cache_directory_exists begin Dir.mkdir(CACHE_DIRECTORY) unless Dir.exist?(CACHE_DIRECTORY) rescue Errno::EACCES, Errno::ENOSPC => e puts "Error creating cache directory: #{e.}" end end |
#get(source_unit, target_unit) ⇒ Numeric|NilClass
Retrieves the conversion factor between source and target units from the cache.
56 57 58 |
# File 'lib/unit_measurements/cache.rb', line 56 def get(source_unit, target_unit) cached_data.dig(source_unit, target_unit) end |
#load_cache ⇒ Hash (private)
Loads the cache from the cache file.
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/unit_measurements/cache.rb', line 98 def load_cache return {} unless File.exist?(@cache_file) begin File.open(@cache_file, "r") { |file| JSON.load(file.read) } rescue Errno::ENOENT, Errno::EACCES, JSON::ParserError => e puts "Error loading cache" {} end end |
#set(source_unit, target_unit, conversion_factor) ⇒ Object
Sets the conversion factor between source and target units in the cache.
68 69 70 71 72 73 |
# File 'lib/unit_measurements/cache.rb', line 68 def set(source_unit, target_unit, conversion_factor) cached_data[source_unit] ||= {} cached_data[source_unit][target_unit] = conversion_factor store_cache end |
#store_cache ⇒ Object (private)
Stores the current cache data to the cache file. cached_data
is stored in prettier form.
122 123 124 125 126 127 128 129 130 |
# File 'lib/unit_measurements/cache.rb', line 122 def store_cache begin File.open(@cache_file, "w") do |file| file.write(JSON.pretty_generate(cached_data)) end rescue Errno::ENOENT, Errno::EACCES, Errno::ENOSPC, JSON::GeneratorError => e puts "Error saving cache: #{e.}" end end |