class RubyUnits::Cache

Performance optimizations to avoid creating units unnecessarily

Attributes

data[RW]

Public Class Methods

new() click to toggle source
# File lib/ruby_units/cache.rb, line 8
def initialize
  clear
end

Public Instance Methods

clear() click to toggle source

Reset the cache

# File lib/ruby_units/cache.rb, line 32
def clear
  @data = {}
end
get(key) click to toggle source

@param key [String, to_unit] @return [RubyUnits::Unit, nil]

# File lib/ruby_units/cache.rb, line 14
def get(key)
  key = key&.to_unit&.units unless key.is_a?(String)
  data[key]
end
keys() click to toggle source

@return [Array<String>]

# File lib/ruby_units/cache.rb, line 27
def keys
  data.keys
end
set(key, value) click to toggle source

@param key [String, to_unit] @return [void]

# File lib/ruby_units/cache.rb, line 21
def set(key, value)
  key = key.to_unit.units unless key.is_a?(String)
  data[key] = value
end