Class: Moneta::Adapters::ActiveRecord
- Inherits:
-
Object
- Object
- Moneta::Adapters::ActiveRecord
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/activerecord.rb
Overview
ActiveRecord as key/value stores
Instance Attribute Summary collapse
- #table ⇒ Object readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from Defaults
#[], #[]=, #close, #decrement, #fetch
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/moneta/adapters/activerecord.rb', line 19 def initialize( = {}) table = [:table] || 'moneta' @table = self.class.tables[table] ||= begin c = Class.new(::ActiveRecord::Base) c.table_name = table c.primary_key = :k c end @table.establish_connection([:connection]) if [:connection] unless @table.table_exists? @table.connection.create_table(@table.table_name, :id => false) do |t| # Do not use binary columns (Issue #17) t.string :k, :null => false t.string :v end @table.connection.add_index(@table.table_name, :k, :unique => true) end end |
Instance Attribute Details
#table ⇒ Object (readonly)
14 15 16 |
# File 'lib/moneta/adapters/activerecord.rb', line 14 def table @table end |
Class Method Details
.tables ⇒ Object
10 11 12 |
# File 'lib/moneta/adapters/activerecord.rb', line 10 def self.tables @tables ||= {} end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
79 80 81 82 |
# File 'lib/moneta/adapters/activerecord.rb', line 79 def clear( = {}) @table.delete_all self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
59 60 61 62 63 64 |
# File 'lib/moneta/adapters/activerecord.rb', line 59 def delete(key, = {}) if record = @table.where(:k => key).first record.destroy record.v end end |
#increment(key, amount = 1, options = {}) ⇒ Object
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically increment integer value with key
This method also accepts negative amounts.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/moneta/adapters/activerecord.rb', line 67 def increment(key, amount = 1, = {}) record = @table.where(:k => key).lock.first_or_initialize value = record.v intvalue = value.to_i raise 'Tried to increment non integer value' unless value == nil || intvalue.to_s == value.to_s intvalue += amount record.v = intvalue.to_s record.save intvalue end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
40 41 42 |
# File 'lib/moneta/adapters/activerecord.rb', line 40 def key?(key, = {}) !@table.where(:k => key).empty? end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
45 46 47 48 |
# File 'lib/moneta/adapters/activerecord.rb', line 45 def load(key, = {}) record = @table.select(:v).where(:k => key).first record && record.v end |
#store(key, value, options = {}) ⇒ Object
Store value with key
51 52 53 54 55 56 |
# File 'lib/moneta/adapters/activerecord.rb', line 51 def store(key, value, = {}) record = @table.select(:k).where(:k => key).first_or_initialize record.v = value record.save value end |