class RiakRecord::Base

Attributes

riak_object[R]

Public Class Methods

all() click to toggle source
# File lib/riak_record/base.rb, line 123
def self.all
  finder.all
end
all_buckets_in_namespace() click to toggle source
# 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() click to toggle source
# File lib/riak_record/base.rb, line 107
def self.bucket
  @bucket ||= client.bucket(bucket_name)
end
bucket_name(name = :not_a_name) click to toggle source
# 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() click to toggle source
# File lib/riak_record/base.rb, line 233
def self.client
  @@client
end
client=(client) click to toggle source
# File lib/riak_record/base.rb, line 229
def self.client=(client)
  @@client = client
end
count() click to toggle source
# File lib/riak_record/base.rb, line 127
def self.count
  finder.count
end
create(*args) click to toggle source
# File lib/riak_record/base.rb, line 59
def self.create(*args)
  self.new(*args).save
end
create!(*args) click to toggle source
# File lib/riak_record/base.rb, line 63
def self.create!(*args)
  self.new(*args).save
end
data_attributes(*attributes) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/riak_record/base.rb, line 119
def self.finder
  finder_class.new(self, :bucket => bucket_name)
end
finder_class() click to toggle source
# File lib/riak_record/base.rb, line 115
def self.finder_class
  @@finder_class ||= RiakRecord::Finder::Basic
end
finder_class=(klass) click to toggle source
# File lib/riak_record/base.rb, line 111
def self.finder_class=(klass)
  @@finder_class = klass
end
first(n = 1) click to toggle source
# File lib/riak_record/base.rb, line 135
def self.first(n = 1)
  finder.first(n)
end
index_bin_attributes(*attributes) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/riak_record/base.rb, line 183
def self.index_names
  @index_names ||= { :bucket => '$bucket' }
end
keys() click to toggle source
# File lib/riak_record/base.rb, line 131
def self.keys
  finder.keys
end
namespace() click to toggle source
# File lib/riak_record/base.rb, line 216
def self.namespace
  @@namespace
end
namespace=(namespace) click to toggle source
# File lib/riak_record/base.rb, line 212
def self.namespace=(namespace)
  @@namespace = namespace
end
namespace_prefixed() click to toggle source
# File lib/riak_record/base.rb, line 220
def self.namespace_prefixed
  self.namespace + ":"
end
new(options = nil) click to toggle source
# 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
page(continuation = nil, page_size = nil) click to toggle source
# File lib/riak_record/base.rb, line 139
def self.page(continuation = nil, page_size = nil)
  finder.page(continuation, page_size)
end
where(options) click to toggle source
# File lib/riak_record/base.rb, line 187
def self.where(options)
  finder_class.new(self, options)
end

Public Instance Methods

==(record) click to toggle source
# 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() click to toggle source
# File lib/riak_record/base.rb, line 30
def data
  riak_object.data
end
delete() click to toggle source
# File lib/riak_record/base.rb, line 67
def delete
  riak_object.delete
end
id() click to toggle source
# File lib/riak_record/base.rb, line 75
def id
  riak_object.key
end
indexes() click to toggle source
# File lib/riak_record/base.rb, line 34
def indexes
  riak_object.indexes
end
new_record?() click to toggle source
# File lib/riak_record/base.rb, line 71
def new_record?
  !(@_stored || riak_object.vclock)
end
reload() click to toggle source
# 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() click to toggle source
# 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
Also aliased as: save!
save!()
Alias for: save
to_param() click to toggle source
# File lib/riak_record/base.rb, line 79
def to_param
  id
end
update_attributes(attributes) click to toggle source
# 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