Class: Swarm::Storage::KeyValueStorage
- Inherits:
-
Object
- Object
- Swarm::Storage::KeyValueStorage
show all
- Defined in:
- lib/swarm/storage/key_value_storage.rb
Defined Under Namespace
Classes: AssociationKeyMissingError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of KeyValueStorage.
10
11
12
|
# File 'lib/swarm/storage/key_value_storage.rb', line 10
def initialize(store)
@store = store
end
|
Instance Attribute Details
#store ⇒ Object
Returns the value of attribute store.
8
9
10
|
# File 'lib/swarm/storage/key_value_storage.rb', line 8
def store
@store
end
|
Instance Method Details
#[](key) ⇒ Object
65
66
67
|
# File 'lib/swarm/storage/key_value_storage.rb', line 65
def [](key)
deserialize(store[key])
end
|
#[]=(key, value) ⇒ Object
69
70
71
|
# File 'lib/swarm/storage/key_value_storage.rb', line 69
def []=(key, value)
store[key] = serialize(value)
end
|
#add_association(association_name, associated, owner:, class_name:, foreign_key: nil) ⇒ Object
34
35
36
37
38
39
40
41
|
# File 'lib/swarm/storage/key_value_storage.rb', line 34
def add_association(association_name, associated, owner:, class_name:, foreign_key: nil)
key = :"#{association_name}_ids"
raise AssociationKeyMissingError, key unless owner.respond_to?(key)
ids = owner.send(key) || owner.send("#{key}=", [])
ids << associated.id
associated
end
|
#all_of_type(_type) ⇒ Object
30
31
32
|
# File 'lib/swarm/storage/key_value_storage.rb', line 30
def all_of_type(_type)
raise "Not implemented yet!"
end
|
#delete(_key) ⇒ Object
73
74
75
|
# File 'lib/swarm/storage/key_value_storage.rb', line 73
def delete(_key)
raise "Not implemented yet!"
end
|
#deserialize(value) ⇒ Object
59
60
61
62
63
|
# File 'lib/swarm/storage/key_value_storage.rb', line 59
def deserialize(value)
return nil if value.nil?
JSON.parse(value)
end
|
#ids_for_type(_type) ⇒ Object
26
27
28
|
# File 'lib/swarm/storage/key_value_storage.rb', line 26
def ids_for_type(_type)
raise "Not implemented yet!"
end
|
#load_associations(association_name, owner:, class_name:, foreign_key: nil) ⇒ Object
43
44
45
46
47
48
49
50
51
|
# File 'lib/swarm/storage/key_value_storage.rb', line 43
def load_associations(association_name, owner:, class_name:, foreign_key: nil)
key = :"#{association_name}_ids"
raise AssociationKeyMissingError, key unless owner.respond_to?(key)
ids = owner.send(key) || []
ids.map { |id|
self["#{class_name.split("::").last}:#{id}"]
}.compact
end
|
#regex_for_type(type) ⇒ Object
22
23
24
|
# File 'lib/swarm/storage/key_value_storage.rb', line 22
def regex_for_type(type)
/^#{type}:(.*)/
end
|
#serialize(value) ⇒ Object
53
54
55
56
57
|
# File 'lib/swarm/storage/key_value_storage.rb', line 53
def serialize(value)
return nil if value.nil?
value.to_json
end
|
#trace ⇒ Object
14
15
16
|
# File 'lib/swarm/storage/key_value_storage.rb', line 14
def trace
self["trace"]
end
|
#trace=(traced) ⇒ Object
18
19
20
|
# File 'lib/swarm/storage/key_value_storage.rb', line 18
def trace=(traced)
self["trace"] = traced
end
|
#truncate ⇒ Object
77
78
79
|
# File 'lib/swarm/storage/key_value_storage.rb', line 77
def truncate
raise "Not implemented yet!"
end
|