Module: EKV::ModelExtensions::KeyValueStore

Defined in:
lib/easy_key_value/key_value_store.rb

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • key (String)

    The key

  • value (String)

    The 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

Parameters:

  • key (String)

    The key we want to destroy

Returns:

  • (Boolean)

    Wether or not the key was destroyed



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

Parameters:

  • key (String)

    The key we want the value of

Returns:

  • (String, nil)

    The value or nil if it does not exist



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

Parameters:

  • key (String)

    The name of the key

  • value (String, nil) (defaults to: nil)

    The value to be set or nil if we just want to read the value

Returns:

  • (String, boolean)

    The value of the key if value is nil



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_storeHash

Gets a hash containing the key and values

Returns:

  • (Hash)

    The hash containing key and values for this object



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

Parameters:

  • key (String)

    The key we want to change

  • value (String)

    The new value for this key

Returns:

  • (Boolean)

    true if the key was updated, false if the key did not exist



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