Class: Moneta::Adapters::ActiveRecord
- Inherits:
-
Object
- Object
- Moneta::Adapters::ActiveRecord
- Includes:
- Defaults, IncrementSupport
- 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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/moneta/adapters/activerecord.rb', line 20 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)
15 16 17 |
# File 'lib/moneta/adapters/activerecord.rb', line 15 def table @table end |
Class Method Details
.tables ⇒ Object
11 12 13 |
# File 'lib/moneta/adapters/activerecord.rb', line 11 def self.tables @tables ||= {} end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
77 78 79 80 |
# File 'lib/moneta/adapters/activerecord.rb', line 77 def clear( = {}) @table.delete_all self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
60 61 62 63 64 65 |
# File 'lib/moneta/adapters/activerecord.rb', line 60 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.
68 69 70 71 72 73 74 |
# File 'lib/moneta/adapters/activerecord.rb', line 68 def increment(key, amount = 1, = {}) record = @table.where(:k => key).lock.first_or_initialize value = convert_for_increment(record.v) + amount record.v = value.to_s record.save value end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
41 42 43 |
# File 'lib/moneta/adapters/activerecord.rb', line 41 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
46 47 48 49 |
# File 'lib/moneta/adapters/activerecord.rb', line 46 def load(key, = {}) record = @table.select(:v).where(:k => key).first record && record.v end |
#store(key, value, options = {}) ⇒ Object
Store value with key
52 53 54 55 56 57 |
# File 'lib/moneta/adapters/activerecord.rb', line 52 def store(key, value, = {}) record = @table.select(:k).where(:k => key).first_or_initialize record.v = value record.save value end |