Class: RubyUnits::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_units/cache.rb

Overview

Performance optimizations to avoid creating units unnecessarily

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



6
7
8
# File 'lib/ruby_units/cache.rb', line 6

def initialize
  clear
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/ruby_units/cache.rb', line 4

def data
  @data
end

Instance Method Details

#clearObject

Reset the cache



30
31
32
# File 'lib/ruby_units/cache.rb', line 30

def clear
  @data = {}
end

#get(key) ⇒ RubyUnits::Unit?

Parameters:

Returns:



12
13
14
15
# File 'lib/ruby_units/cache.rb', line 12

def get(key)
  key = key&.to_unit&.units unless key.is_a?(String)
  data[key]
end

#keysArray<String>

Returns:



25
26
27
# File 'lib/ruby_units/cache.rb', line 25

def keys
  data.keys
end

#set(key, value) ⇒ void

This method returns an undefined value.

Parameters:



19
20
21
22
# File 'lib/ruby_units/cache.rb', line 19

def set(key, value)
  key = key.to_unit.units unless key.is_a?(String)
  data[key] = value
end