Class: RiakRecord::Base

Inherits:
Object
  • Object
show all
Includes:
Associations, Callbacks
Defined in:
lib/riak_record/base.rb

Constant Summary collapse

@@namespace =
nil

Constants included from Callbacks

Callbacks::CALLBACK_TRIGGERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations

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_objectObject (readonly)

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

.allObject



123
124
125
# File 'lib/riak_record/base.rb', line 123

def self.all
  finder.all
end

.all_buckets_in_namespaceObject



220
221
222
223
# File 'lib/riak_record/base.rb', line 220

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

.bucketObject



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

.clientObject



229
230
231
# File 'lib/riak_record/base.rb', line 229

def self.client
  @@client
end

.client=(client) ⇒ Object



225
226
227
# File 'lib/riak_record/base.rb', line 225

def self.client=(client)
  @@client = client
end

.countObject



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



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/riak_record/base.rb', line 139

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



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/riak_record/base.rb', line 187

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



201
202
203
204
205
# File 'lib/riak_record/base.rb', line 201

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

.finderObject



119
120
121
# File 'lib/riak_record/base.rb', line 119

def self.finder
  finder_class.new(self, :bucket => bucket_name)
end

.finder_classObject



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



131
132
133
# File 'lib/riak_record/base.rb', line 131

def self.first(n = 1)
  finder.first(n)
end

.index_bin_attributes(*attributes) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/riak_record/base.rb', line 165

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



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/riak_record/base.rb', line 151

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_namesObject



179
180
181
# File 'lib/riak_record/base.rb', line 179

def self.index_names
  @index_names ||= { :bucket => '$bucket' }
end

.namespaceObject



212
213
214
# File 'lib/riak_record/base.rb', line 212

def self.namespace
  @@namespace
end

.namespace=(namespace) ⇒ Object



208
209
210
# File 'lib/riak_record/base.rb', line 208

def self.namespace=(namespace)
  @@namespace = namespace
end

.namespace_prefixedObject



216
217
218
# File 'lib/riak_record/base.rb', line 216

def self.namespace_prefixed
  self.namespace + ":"
end

.page(continuation = nil, page_size = nil) ⇒ Object



135
136
137
# File 'lib/riak_record/base.rb', line 135

def self.page(continuation = nil, page_size = nil)
  finder.page(continuation, page_size)
end

.where(options) ⇒ Object



183
184
185
# File 'lib/riak_record/base.rb', line 183

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

#dataObject



30
31
32
# File 'lib/riak_record/base.rb', line 30

def data
  riak_object.data
end

#deleteObject



67
68
69
# File 'lib/riak_record/base.rb', line 67

def delete
  riak_object.delete
end

#idObject



75
76
77
# File 'lib/riak_record/base.rb', line 75

def id
  riak_object.key
end

#indexesObject



34
35
36
# File 'lib/riak_record/base.rb', line 34

def indexes
  riak_object.indexes
end


38
39
40
# File 'lib/riak_record/base.rb', line 38

def links
  riak_object.links
end

#new_record?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/riak_record/base.rb', line 71

def new_record?
  !(@_stored || riak_object.vclock)
end

#reloadObject



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

#saveObject 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_paramObject



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