Class: Yoda::Store::Objects::Map
- Inherits:
-
Object
- Object
- Yoda::Store::Objects::Map
- Defined in:
- lib/yoda/store/objects/map.rb
Instance Attribute Summary collapse
- #adapter ⇒ Adapter readonly
- #cache ⇒ Hash readonly
- #path ⇒ String readonly
- #updated_keys ⇒ Set readonly
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(adapter:, path:) ⇒ Map
constructor
A new instance of Map.
- #keys ⇒ Object
- #save ⇒ Object
- #separator ⇒ Object
Constructor Details
#initialize(adapter:, path:) ⇒ Map
Returns a new instance of Map.
20 21 22 23 24 25 |
# File 'lib/yoda/store/objects/map.rb', line 20 def initialize(adapter:, path:) @adapter = adapter @path = path @cache = {} @updated_keys = Set.new end |
Instance Attribute Details
#adapter ⇒ Adapter (readonly)
10 11 12 |
# File 'lib/yoda/store/objects/map.rb', line 10 def adapter @adapter end |
#cache ⇒ Hash (readonly)
13 14 15 |
# File 'lib/yoda/store/objects/map.rb', line 13 def cache @cache end |
#path ⇒ String (readonly)
7 8 9 |
# File 'lib/yoda/store/objects/map.rb', line 7 def path @path end |
#updated_keys ⇒ Set (readonly)
16 17 18 |
# File 'lib/yoda/store/objects/map.rb', line 16 def updated_keys @updated_keys end |
Instance Method Details
#[](key) ⇒ Object
36 37 38 39 40 |
# File 'lib/yoda/store/objects/map.rb', line 36 def [](key) cache.fetch(key.to_sym) do cache[key.to_sym] = adapter.get(path_for(key)) end end |
#[]=(key, value) ⇒ Object
31 32 33 34 |
# File 'lib/yoda/store/objects/map.rb', line 31 def []=(key, value) updated_keys.add(key.to_sym) cache[key.to_sym] = value end |
#keys ⇒ Object
48 49 50 51 |
# File 'lib/yoda/store/objects/map.rb', line 48 def keys @adapter_keys ||= adapter.keys.select { |key| key.start_with?("#{path}#{separator}") }.map { |key| key.slice(("#{path}#{separator}".length)..-1).to_sym } Set.new(@adapter_keys) + updated_keys end |
#save ⇒ Object
42 43 44 45 46 |
# File 'lib/yoda/store/objects/map.rb', line 42 def save updated_keys.each do |key| adapter.put(path_for(key), self[key.to_sym]) end end |
#separator ⇒ Object
27 28 29 |
# File 'lib/yoda/store/objects/map.rb', line 27 def separator '::' end |