Class: Swarm::Storage::KeyValueStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm/storage/key_value_storage.rb

Direct Known Subclasses

HashStorage, RedisStorage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ KeyValueStorage

Returns a new instance of KeyValueStorage.



6
7
8
# File 'lib/swarm/storage/key_value_storage.rb', line 6

def initialize(store)
  @store = store
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



4
5
6
# File 'lib/swarm/storage/key_value_storage.rb', line 4

def store
  @store
end

Instance Method Details

#[](key) ⇒ Object



51
52
53
# File 'lib/swarm/storage/key_value_storage.rb', line 51

def [](key)
  deserialize(store[key])
end

#[]=(key, value) ⇒ Object



55
56
57
# File 'lib/swarm/storage/key_value_storage.rb', line 55

def []=(key, value)
  store[key] = serialize(value)
end

#all_of_type(type) ⇒ Object



26
27
28
# File 'lib/swarm/storage/key_value_storage.rb', line 26

def all_of_type(type)
  raise "Not implemented yet!"
end

#delete(key) ⇒ Object



59
60
61
# File 'lib/swarm/storage/key_value_storage.rb', line 59

def delete(key)
  raise "Not implemented yet!"
end

#deserialize(value) ⇒ Object



46
47
48
49
# File 'lib/swarm/storage/key_value_storage.rb', line 46

def deserialize(value)
  return nil if value.nil?
  JSON.parse(value)
end

#ids_for_type(type) ⇒ Object



22
23
24
# File 'lib/swarm/storage/key_value_storage.rb', line 22

def ids_for_type(type)
  raise "Not implemented yet!"
end

#load_associations(association_name, owner:, type:, foreign_key: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/swarm/storage/key_value_storage.rb', line 30

def load_associations(association_name, owner:, type:, foreign_key: nil)
  type = type.split("::").last
  local_association_ids = :"#{association_name}_ids"
  if owner.respond_to?(local_association_ids)
    ids = owner.send(local_association_ids) || []
    ids.map { |id|
      self["#{type}:#{id}"]
    }.compact
  end
end

#regex_for_type(type) ⇒ Object



18
19
20
# File 'lib/swarm/storage/key_value_storage.rb', line 18

def regex_for_type(type)
  /^#{type}\:(.*)/
end

#serialize(value) ⇒ Object



41
42
43
44
# File 'lib/swarm/storage/key_value_storage.rb', line 41

def serialize(value)
  return nil if value.nil?
  value.to_json
end

#traceObject



10
11
12
# File 'lib/swarm/storage/key_value_storage.rb', line 10

def trace
  self["trace"]
end

#trace=(traced) ⇒ Object



14
15
16
# File 'lib/swarm/storage/key_value_storage.rb', line 14

def trace=(traced)
  self["trace"] = traced
end

#truncateObject



63
64
65
# File 'lib/swarm/storage/key_value_storage.rb', line 63

def truncate
  raise "Not implemented yet!"
end