Class: UnitMeasurements::Cache

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

Author:

Since:

  • 5.2.0

Constant Summary collapse

CACHE_DIRECTORY =

The directory path where cache files are stored.

Author:

Since:

  • 5.2.0

File.expand_path(File.join("..", "..", "cache"), __dir__).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • unit_group (UnitGroup)

    The unit group associated with the cache.

Author:

Since:

  • 5.2.0



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_dataHash (readonly)

Stores cached conversion factors between different units within a unit group.

Returns:

  • (Hash)

    The cached conversion factors.

Author:

Since:

  • 5.2.0



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.

Parameters:

  • unit_group (UnitGroup)

    The unit group associated with the cache.

Returns:

  • (String)

    An absolute path of the cache file.

Author:

Since:

  • 5.2.0



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_cacheObject

Clears the entire cache.

Author:

Since:

  • 5.2.0



79
80
81
82
# File 'lib/unit_measurements/cache.rb', line 79

def clear_cache
  @cached_data = {}
  store_cache
end

#ensure_cache_directory_existsObject (private)

Ensures that the cache directory exists. If the cache directory does not exist, it gets created.

Raises:

  • (Errno::EEXIST)

    If the cache directory already exists.

  • (Errno::EACCES)

    If the cache directory cannot be accessed due to insufficient permissions.

  • (Errno::ENOSPC)

    If there’s not enough space to create the cache directory.

Author:

Since:

  • 5.2.0



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.message}"
  end
end

#get(source_unit, target_unit) ⇒ Numeric|NilClass

Retrieves the conversion factor between source and target units from the cache.

Parameters:

  • source_unit (String)

    The source unit name.

  • target_unit (String)

    The target unit name.

Returns:

  • (Numeric|NilClass)

    The conversion factor, or nil if not found in the cache.

Author:

Since:

  • 5.2.0



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_cacheHash (private)

Loads the cache from the cache file.

Returns:

  • (Hash)

    The loaded cache data.

Raises:

  • (Errno::ENOENT)

    If the cache file does not exist.

  • (Errno::EACCES)

    If the cache file cannot be accessed due to insufficient permissions.

  • (JSON::ParserError)

    If there’s an error parsing the cache file.

Author:

Since:

  • 5.2.0



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.

Parameters:

  • source_unit (String)

    The source unit name.

  • target_unit (String)

    The target unit name.

  • conversion_factor (Numeric)

    The conversion factor.

Author:

Since:

  • 5.2.0



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_cacheObject (private)

Stores the current cache data to the cache file. cached_data is stored in prettier form.

Raises:

  • (Errno::ENOENT)

    If the cache file does not exist.

  • (Errno::EACCES)

    If the cache file cannot be accessed due to insufficient permissions.

  • (Errno::ENOSPC)

    If there’s not enough space to write to the cache file.

  • (JSON::GeneratorError)

    If there’s an error generating JSON data.

Author:

Since:

  • 5.2.0



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.message}"
  end
end