Class: RiakRecord::Base
Constant Summary
collapse
- @@namespace =
nil
Constants included
from Callbacks
Callbacks::CALLBACK_TRIGGERS
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
included, #link_definitions, #update_links
Methods included from Callbacks
#call_callbacks!, included
Constructor Details
#initialize(options = nil) ⇒ Base
Returns a new instance of Base.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/riak_record/base.rb', line 13
def initialize(options = nil)
if options.is_a?(Riak::RObject)
@riak_object = options
else
@riak_object = self.class.bucket.new
@riak_object.content_type = 'application/json'
@riak_object.data = {}
if options.is_a?(Hash)
id = options.delete(:id) || options.delete(:key)
@riak_object.key = id.to_s if id
options.each_pair{ |k,v| self.send("#{k}=".to_sym, v) }
elsif !options.nil?
@riak_object.key = options.to_s
end
end
end
|
Instance Attribute Details
#riak_object ⇒ Object
Returns the value of attribute riak_object.
5
6
7
|
# File 'lib/riak_record/base.rb', line 5
def riak_object
@riak_object
end
|
Class Method Details
.all ⇒ Object
123
124
125
|
# File 'lib/riak_record/base.rb', line 123
def self.all
finder.all
end
|
.all_buckets_in_namespace ⇒ Object
224
225
226
227
|
# File 'lib/riak_record/base.rb', line 224
def self.all_buckets_in_namespace
raise "namespace not set" unless self.namespace
client.list_buckets.select{|b| b.name.match(/^#{ Regexp.escape(self.namespace_prefixed) }/) }
end
|
.bucket ⇒ Object
107
108
109
|
# File 'lib/riak_record/base.rb', line 107
def self.bucket
@bucket ||= client.bucket(bucket_name)
end
|
.bucket_name(name = :not_a_name) ⇒ Object
102
103
104
105
|
# File 'lib/riak_record/base.rb', line 102
def self.bucket_name(name = :not_a_name)
@bucket_name = name.to_s unless name == :not_a_name
namespace.present? ? namespace_prefixed+@bucket_name : @bucket_name
end
|
.client ⇒ Object
233
234
235
|
# File 'lib/riak_record/base.rb', line 233
def self.client
@@client
end
|
.client=(client) ⇒ Object
229
230
231
|
# File 'lib/riak_record/base.rb', line 229
def self.client=(client)
@@client = client
end
|
.count ⇒ Object
127
128
129
|
# File 'lib/riak_record/base.rb', line 127
def self.count
finder.count
end
|
.create(*args) ⇒ Object
59
60
61
|
# File 'lib/riak_record/base.rb', line 59
def self.create(*args)
self.new(*args).save
end
|
.create!(*args) ⇒ Object
63
64
65
|
# File 'lib/riak_record/base.rb', line 63
def self.create!(*args)
self.new(*args).save
end
|
.data_attributes(*attributes) ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/riak_record/base.rb', line 143
def self.data_attributes(*attributes)
attributes.map(&:to_sym).each do |method_name|
define_method(method_name) do
data[method_name.to_s]
end
define_method("#{method_name}=".to_sym) do |value|
data[method_name.to_s] = value
end
end
end
|
.find(key_or_keys) ⇒ Object
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/riak_record/base.rb', line 191
def self.find(key_or_keys)
return find_many(key_or_keys) if key_or_keys.is_a?(Array)
begin
self.new(bucket.get(key_or_keys.to_s))
rescue Riak::FailedRequest => e
if e.not_found?
nil
else
raise e
end
end
end
|
.find_many(keys) ⇒ Object
205
206
207
208
209
|
# File 'lib/riak_record/base.rb', line 205
def self.find_many(keys)
hash = bucket.get_many(keys.map(&:to_s))
values = keys.map{ |k| hash[k] }.compact
values.map{|robject| new(robject) }
end
|
.finder ⇒ Object
119
120
121
|
# File 'lib/riak_record/base.rb', line 119
def self.finder
finder_class.new(self, :bucket => bucket_name)
end
|
.finder_class ⇒ Object
115
116
117
|
# File 'lib/riak_record/base.rb', line 115
def self.finder_class
@@finder_class ||= RiakRecord::Finder::Basic
end
|
.finder_class=(klass) ⇒ Object
111
112
113
|
# File 'lib/riak_record/base.rb', line 111
def self.finder_class=(klass)
@@finder_class = klass
end
|
.first(n = 1) ⇒ Object
135
136
137
|
# File 'lib/riak_record/base.rb', line 135
def self.first(n = 1)
finder.first(n)
end
|
.index_bin_attributes(*attributes) ⇒ Object
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/riak_record/base.rb', line 169
def self.index_bin_attributes(*attributes)
attributes.map(&:to_sym).each do |method_name|
index_names[method_name.to_sym] = "#{method_name}_bin"
define_method(method_name) do
indexes["#{method_name}_bin"].to_a
end
define_method("#{method_name}=".to_sym) do |value|
indexes["#{method_name}_bin"] = Array(value).map(&:to_s)
end
end
end
|
.index_int_attributes(*attributes) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/riak_record/base.rb', line 155
def self.index_int_attributes(*attributes)
attributes.map(&:to_sym).each do |method_name|
index_names[method_name.to_sym] = "#{method_name}_int"
define_method(method_name) do
indexes["#{method_name}_int"].to_a
end
define_method("#{method_name}=".to_sym) do |value|
indexes["#{method_name}_int"] = Array(value).map(&:to_i)
end
end
end
|
.index_names ⇒ Object
183
184
185
|
# File 'lib/riak_record/base.rb', line 183
def self.index_names
@index_names ||= { :bucket => '$bucket' }
end
|
.keys ⇒ Object
131
132
133
|
# File 'lib/riak_record/base.rb', line 131
def self.keys
finder.keys
end
|
.namespace ⇒ Object
216
217
218
|
# File 'lib/riak_record/base.rb', line 216
def self.namespace
@@namespace
end
|
.namespace=(namespace) ⇒ Object
212
213
214
|
# File 'lib/riak_record/base.rb', line 212
def self.namespace=(namespace)
@@namespace = namespace
end
|
.namespace_prefixed ⇒ Object
220
221
222
|
# File 'lib/riak_record/base.rb', line 220
def self.namespace_prefixed
self.namespace + ":"
end
|
.page(continuation = nil, page_size = nil) ⇒ Object
139
140
141
|
# File 'lib/riak_record/base.rb', line 139
def self.page(continuation = nil, page_size = nil)
finder.page(continuation, page_size)
end
|
.where(options) ⇒ Object
187
188
189
|
# File 'lib/riak_record/base.rb', line 187
def self.where(options)
finder_class.new(self, options)
end
|
Instance Method Details
#==(record) ⇒ Object
83
84
85
86
|
# File 'lib/riak_record/base.rb', line 83
def ==(record)
return false unless record.kind_of?(RiakRecord::Base)
self.class.bucket_name == record.class.bucket_name && id == record.id
end
|
#data ⇒ Object
30
31
32
|
# File 'lib/riak_record/base.rb', line 30
def data
riak_object.data
end
|
#delete ⇒ Object
67
68
69
|
# File 'lib/riak_record/base.rb', line 67
def delete
riak_object.delete
end
|
#id ⇒ Object
75
76
77
|
# File 'lib/riak_record/base.rb', line 75
def id
riak_object.key
end
|
#indexes ⇒ Object
34
35
36
|
# File 'lib/riak_record/base.rb', line 34
def indexes
riak_object.indexes
end
|
#links ⇒ Object
38
39
40
|
# File 'lib/riak_record/base.rb', line 38
def links
riak_object.links
end
|
#new_record? ⇒ Boolean
71
72
73
|
# File 'lib/riak_record/base.rb', line 71
def new_record?
!(@_stored || riak_object.vclock)
end
|
#reload ⇒ Object
88
89
90
91
92
|
# File 'lib/riak_record/base.rb', line 88
def reload
@riak_object = self.class.bucket.get(id)
@riak_object.data = {} if @riak_object.data.nil?
self
end
|
#save ⇒ Object
Also known as:
save!
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/riak_record/base.rb', line 42
def save
creating = new_record?
before_save!
creating ? before_create! : before_update!
update_links
riak_object.store(:returnbody => false)
creating ? after_create! : after_update!
after_save!
@_stored = true
self
end
|
#to_param ⇒ Object
79
80
81
|
# File 'lib/riak_record/base.rb', line 79
def to_param
id
end
|
#update_attributes(attributes) ⇒ Object
94
95
96
97
98
99
100
|
# File 'lib/riak_record/base.rb', line 94
def update_attributes(attributes)
attributes.each_pair do |k,v|
setter = "#{k}=".to_sym
self.send(setter, v) if respond_to?(setter)
end
save
end
|