Class: Moneta::Adapters::ActiveRecord

Inherits:
Base
  • Object
show all
Defined in:
lib/moneta/adapters/activerecord.rb

Overview

ActiveRecord as key/value stores

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #[]=, #close, #decrement, #fetch

Methods included from Mixins::WithOptions

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ ActiveRecord

Constructor

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :table (String) — default: 'moneta'

    Table name

  • :connection (Hash)

    ActiveRecord connection configuration



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(options = {})
  table = options[: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(options[:connection]) if options[: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

#tableObject (readonly)



12
13
14
# File 'lib/moneta/adapters/activerecord.rb', line 12

def table
  @table
end

Class Method Details

.tablesObject



8
9
10
# File 'lib/moneta/adapters/activerecord.rb', line 8

def self.tables
  @tables ||= {}
end

Instance Method Details

#clear(options = {}) ⇒ Object



73
74
75
76
# File 'lib/moneta/adapters/activerecord.rb', line 73

def clear(options = {})
  @table.delete_all
  self
end

#delete(key, options = {}) ⇒ Object



55
56
57
58
59
60
# File 'lib/moneta/adapters/activerecord.rb', line 55

def delete(key, options = {})
  if record = @table.where(:k => key).first
    record.destroy
    record.v
  end
end

#increment(key, amount = 1, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/moneta/adapters/activerecord.rb', line 62

def increment(key, amount = 1, options = {})
  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

Returns:

  • (Boolean)


39
40
41
# File 'lib/moneta/adapters/activerecord.rb', line 39

def key?(key, options = {})
  !@table.where(:k => key).empty?
end

#load(key, options = {}) ⇒ Object



43
44
45
46
# File 'lib/moneta/adapters/activerecord.rb', line 43

def load(key, options = {})
  record = @table.select(:v).where(:k => key).first
  record && record.v
end

#store(key, value, options = {}) ⇒ Object



48
49
50
51
52
53
# File 'lib/moneta/adapters/activerecord.rb', line 48

def store(key, value, options = {})
  record = @table.select(:k).where(:k => key).first_or_initialize
  record.v = value
  record.save
  value
end