Class: Yoda::Store::Objects::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/store/objects/map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:, path:) ⇒ Map

Returns a new instance of Map.

Parameters:



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

#adapterAdapter (readonly)

Returns:

  • (Adapter)


10
11
12
# File 'lib/yoda/store/objects/map.rb', line 10

def adapter
  @adapter
end

#cacheHash (readonly)

Returns:

  • (Hash)


13
14
15
# File 'lib/yoda/store/objects/map.rb', line 13

def cache
  @cache
end

#pathString (readonly)

Returns:

  • (String)


7
8
9
# File 'lib/yoda/store/objects/map.rb', line 7

def path
  @path
end

#updated_keysSet (readonly)

Returns:

  • (Set)


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

#keysObject



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

#saveObject



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

#separatorObject



27
28
29
# File 'lib/yoda/store/objects/map.rb', line 27

def separator
  '::'
end