Class: ArKeyValueStore
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- ArKeyValueStore
- Defined in:
- app/models/ar_key_value_store.rb
Overview
Schema information
Table name: ar_key_value_store : Table used for storing all kind of values
id Integer id
key String Identification key
value String Stored value
This model represents key/value store. Typical usage is for saving last used document number on internal document numbering schema.
Example:
invoice_number = ArKeyValueStore.get_next_value('invoices', invoice.year)
Class Method Summary collapse
-
.get_next_value(*keys) ⇒ Object
Will return value incremented by 1 and update document with new value.
-
.get_value(*keys) ⇒ Object
Will return current value for the key.
-
.peep_next_value(*keys) ⇒ Object
Will return value incremented by 1 but will not update document with new value.
-
.restore_value(value, *keys) ⇒ Object
Will try to restore to previous value if value matches current value.
-
.set_value(value, *keys) ⇒ Object
Will set value for the key.
Class Method Details
.get_next_value(*keys) ⇒ Object
Will return value incremented by 1 and update document with new value.
Parameters:
- keys
-
Array. Any number of parameters from which key will be generated.
Returns: String. Last saved value incremented by 1.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/models/ar_key_value_store.rb', line 50 def self.get_next_value(*keys) doc = find_by(key: keys.join('-')) value = (doc ? doc.value : '0').next if doc doc.value = value doc.save! else create(key: keys.join('-'), value: value) end value end |
.get_value(*keys) ⇒ Object
Will return current value for the key.
Parameters:
- keys
-
Array. Any number of parameters from which key will be generated.
Returns: String. Current value for specified key or nil if key is not found.
105 106 107 108 |
# File 'app/models/ar_key_value_store.rb', line 105 def self.get_value(*keys) doc = find_by(key: keys.join('-')) doc ? doc.value : nil end |
.peep_next_value(*keys) ⇒ Object
Will return value incremented by 1 but will not update document with new value. Used for presenting user with most possible document number. Real document number must of course be obtained by get_next_value before document is saved to collection.
Parameters:
- keys
-
Array. Any number of parameters from which key will be generated.
Returns: String. Last saved value incremented by 1.
91 92 93 94 |
# File 'app/models/ar_key_value_store.rb', line 91 def self.peep_next_value(*keys) doc = find_by(key: keys.join('-')) (doc ? doc.value : '0').next end |
.restore_value(value, *keys) ⇒ Object
Will try to restore to previous value if value matches current value.
Parameters:
- value
-
String. Last value obtained by get_next_value method.
- keys
-
Array. Any number of parameters from which key will be generated.
69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/ar_key_value_store.rb', line 69 def self.restore_value(value, *keys) doc = find_by(key: keys.join('-')) if value == doc.value value = value.prev doc.value = value doc.save! return value end nil end |
.set_value(value, *keys) ⇒ Object
Will set value for the key. If document is not found new document will be created.
Parameters:
- value
-
String. New value to be set.
- keys
-
Array. Any number of parameters from which key will be generated.
117 118 119 120 121 122 123 124 125 126 |
# File 'app/models/ar_key_value_store.rb', line 117 def self.set_value(value, *keys) doc = find_by(key: keys.join('-')) if doc doc.value = value doc.save! else create(key: keys.join('-'), value: value.to_s) end value end |