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.



8
9
10
# File 'lib/ruby_units/cache.rb', line 8

def initialize
  clear
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

Instance Method Details

#clearObject

Reset the cache



32
33
34
# File 'lib/ruby_units/cache.rb', line 32

def clear
  @data = {}
end

#get(key) ⇒ RubyUnits::Unit?

Parameters:

Returns:



14
15
16
17
# File 'lib/ruby_units/cache.rb', line 14

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

#keysArray<String>

Returns:



27
28
29
# File 'lib/ruby_units/cache.rb', line 27

def keys
  data.keys
end

#set(key, value) ⇒ void

This method returns an undefined value.

Parameters:



21
22
23
24
# 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