Module: EKV::ModelExtensions::KeyValueStore
- Defined in:
- lib/easy_key_value/key_value_store.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#add_key(key, value) ⇒ Object
Adds a key with its value.
-
#del_key(key) ⇒ Boolean
Removes a given key.
-
#get_key(key) ⇒ String?
Returns the value of a given key.
-
#key(key, value = nil) ⇒ String, boolean
Accesses a key, or sets / updates it depending on its existence.
-
#kv_store ⇒ Hash
Gets a hash containing the key and values.
-
#update_key(key, value) ⇒ Boolean
Updates a given key with the given value.
Class Method Details
.included(base) ⇒ Object
5 6 7 8 |
# File 'lib/easy_key_value/key_value_store.rb', line 5 def self.included(base) class_variable_set(:@@ekv_defaults, {}) base.send(:extend, ModelExtensions::ClassMethods) end |
Instance Method Details
#add_key(key, value) ⇒ Object
Adds a key with its value
33 34 35 36 37 38 39 40 |
# File 'lib/easy_key_value/key_value_store.rb', line 33 def add_key(key, value) self.load_ekv if EasyKeyValue.add_key(self, key, value) == true @ekv[key] = value return true end false end |
#del_key(key) ⇒ Boolean
Removes a given key
55 56 57 58 59 60 61 62 63 |
# File 'lib/easy_key_value/key_value_store.rb', line 55 def del_key(key) self.load_ekv return false unless @ekv.key? key ekv = EasyKeyValue.find_by_ekv_id_and_ekv_type_and_key(self.id, self.class.to_s, key) return false if ekv.nil? ekv.destroy @ekv.delete key true end |
#get_key(key) ⇒ String?
Returns the value of a given key
46 47 48 49 |
# File 'lib/easy_key_value/key_value_store.rb', line 46 def get_key(key) self.load_ekv @ekv[key] || @@ekv_defaults[key] end |
#key(key, value = nil) ⇒ String, boolean
Accesses a key, or sets / updates it depending on its existence
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/easy_key_value/key_value_store.rb', line 15 def key(key, value = nil) self.load_ekv return self.get_key(key) if value.nil? if @ekv.key? key #Update self.update_key(key, value) else # Add a key self.add_key(key, value) end end |
#kv_store ⇒ Hash
Gets a hash containing the key and values
83 84 85 86 |
# File 'lib/easy_key_value/key_value_store.rb', line 83 def kv_store self.load_ekv @ekv.clone.freeze end |
#update_key(key, value) ⇒ Boolean
Updates a given key with the given value
70 71 72 73 74 75 76 77 78 |
# File 'lib/easy_key_value/key_value_store.rb', line 70 def update_key(key, value) return false unless @ekv.key? key ekv = EasyKeyValue.find_by_ekv_id_and_ekv_type_and_key(self.id, self.class.to_s, key) return false if ekv.nil? ekv.value = value ekv.save @ekv[key] = value true end |