Class: RubyUnits::Cache
- Inherits:
-
Object
- Object
- RubyUnits::Cache
- Defined in:
- lib/ruby_units/cache.rb
Overview
Performance optimizations to avoid creating units unnecessarily
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Instance Method Summary collapse
-
#clear ⇒ Object
Reset the cache.
- #get(key) ⇒ RubyUnits::Unit?
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #keys ⇒ Array<String>
- #set(key, value) ⇒ void
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
8 9 10 |
# File 'lib/ruby_units/cache.rb', line 8 def initialize clear end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
6 7 8 |
# File 'lib/ruby_units/cache.rb', line 6 def data @data end |
Instance Method Details
#clear ⇒ Object
Reset the cache
32 33 34 |
# File 'lib/ruby_units/cache.rb', line 32 def clear @data = {} end |
#get(key) ⇒ RubyUnits::Unit?
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 |
#set(key, value) ⇒ void
This method returns an undefined value.
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 |