Class: DcKeyValueStore

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
app/models/dc_key_value_store.rb

Overview

Schema information

Collection name: dc_key_value_store : Table used for storing all kind of values

_id                  BSON::ObjectId       _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:

doc_number = DcKeyValueStore.get_next_value('invoices', invoice_date.year)

Class Method Summary collapse

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.



56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/dc_key_value_store.rb', line 56

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.



111
112
113
114
# File 'app/models/dc_key_value_store.rb', line 111

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.



97
98
99
100
# File 'app/models/dc_key_value_store.rb', line 97

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 is not already incremented.

Parameters:

value

String. Last value obtained by get_next_value method.

keys

Array. Any number of parameters from which key will be generated.



75
76
77
78
79
80
81
82
83
84
# File 'app/models/dc_key_value_store.rb', line 75

def self.restore_value(value, *keys)
  doc = find_by(key: keys.join('-'))
  if value == doc.value
    value = (value.to_i - 1).to_s
    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.



123
124
125
126
127
128
129
130
131
132
# File 'app/models/dc_key_value_store.rb', line 123

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)
  end
  value
end