Class: Juno::Adapters::ActiveRecord
- Inherits:
-
Base
- Object
- Base
- Juno::Adapters::ActiveRecord
show all
- Defined in:
- lib/juno/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, #fetch
Constructor Details
#initialize(options = {}) ⇒ ActiveRecord
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/juno/adapters/activerecord.rb', line 21
def initialize(options = {})
table = options[:table] || 'juno'
@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|
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
12
13
14
|
# File 'lib/juno/adapters/activerecord.rb', line 12
def table
@table
end
|
Class Method Details
.tables ⇒ Object
8
9
10
|
# File 'lib/juno/adapters/activerecord.rb', line 8
def self.tables
@tables ||= {}
end
|
Instance Method Details
#clear(options = {}) ⇒ Object
68
69
70
71
|
# File 'lib/juno/adapters/activerecord.rb', line 68
def clear(options = {})
@table.delete_all
self
end
|
#delete(key, options = {}) ⇒ Object
50
51
52
53
54
55
56
57
58
|
# File 'lib/juno/adapters/activerecord.rb', line 50
def delete(key, options = {})
@table.transaction do
record = @table.find_by_k(key)
if record
record.destroy
record.v
end
end
end
|
#key?(key, options = {}) ⇒ Boolean
41
42
43
|
# File 'lib/juno/adapters/activerecord.rb', line 41
def key?(key, options = {})
!!@table.find_by_k(key)
end
|
#load(key, options = {}) ⇒ Object
45
46
47
48
|
# File 'lib/juno/adapters/activerecord.rb', line 45
def load(key, options = {})
record = @table.find_by_k(key)
record ? record.v : nil
end
|
#store(key, value, options = {}) ⇒ Object
60
61
62
63
64
65
66
|
# File 'lib/juno/adapters/activerecord.rb', line 60
def store(key, value, options = {})
@table.transaction do
record = @table.find_or_initialize_by_k(key)
record.update_attributes(:v => value)
value
end
end
|