module Ork::Document

Attributes

id[RW]

Public Class Methods

included(klass) click to toggle source
# File lib/ork/model/document.rb, line 8
def self.included(klass)
  klass.send(:include, Ork::Model)
  klass.extend(Ork::Model::Finders)
end

Public Instance Methods

==(other) click to toggle source

Check for equality by doing the following assertions:

  1. That the passed model is of the same type.

  2. That they represent the same RObject id.

# File lib/ork/model/document.rb, line 18
def ==(other)
  other.kind_of?(model) && other.id == id
end
Also aliased as: eql?
delete() click to toggle source

Delete the model

# File lib/ork/model/document.rb, line 91
def delete
  __robject.delete unless new?
  freeze
rescue Riak::FailedRequest
  false
end
embeddable?() click to toggle source
# File lib/ork/model/document.rb, line 27
def embeddable?
  false
end
eql?(other)
Alias for: ==
inspect() click to toggle source

Pretty print for the model

Example:

  User.new(name: 'John').inspect
  # => #<User:6kS5VHNbaed9h7gFLnVg5lmO4U7 {:name=>"John"}>
# File lib/ork/model/document.rb, line 37
def inspect
  "#<#{model}:#{id || 'nil'} #{attributes.inspect}>"
end
new?() click to toggle source
# File lib/ork/model/document.rb, line 23
def new?
  !id
end
reload() click to toggle source

Preload all the attributes of this model from Riak.

# File lib/ork/model/document.rb, line 86
def reload
  new? ? self : self.load!(@id)
end
save() click to toggle source

Persist the model attributes and update indices and unique indices.

Example:

class User
  include Ork::Document

  attribute :name
end

u = User.new(:name => "John").save
# => #<User:6kS5VHNbaed9h7gFLnVg5lmO4U7 {:name=>"John"}>
# File lib/ork/model/document.rb, line 72
def save
  __robject.content_type = model.content_type
  __robject.data = __persist_attributes

  __check_unique_indices
  __update_indices
  __robject.store

  @id = __robject.key

  self
end
update(attributes) click to toggle source

Update the model attributes and call save.

Example:

User[1].update(:name => "John")

# It's the same as:

u = User[1]
u.update_attributes(:name => "John")
u.save
# File lib/ork/model/document.rb, line 53
def update(attributes)
  update_attributes(attributes)
  save
end

Protected Instance Methods

__check_unique_indices() click to toggle source

Look up into Riak for repeated values on unique attributes

# File lib/ork/model/document.rb, line 139
def __check_unique_indices
  model.uniques.each do |uniq|
    if value = attributes[uniq]
      index = model.indices[uniq]
      records = model.bucket.get_index(index.riak_name, value)
      unless records.empty? || records == [self.id]
        raise Ork::UniqueIndexViolation, "#{uniq} is not unique"
      end
    end
  end
end
__load_robject!(id, robject) click to toggle source

Transform a RObject returned by Riak into a Ork::Document.

# File lib/ork/model/document.rb, line 109
def __load_robject!(id, robject)
  @id = id
  @__robject = robject
  @attributes = {}
  @embedding = {}
  @_memo = {}

  data = @__robject.data
  embedded_data = {}

  model.embedding.each do |embedded|
    if d = data.delete(embedded.to_s)
      embedded_data[embedded] = d
    end
  end

  update_attributes data
  update_embedded_attributes embedded_data

  self
end
__robject() click to toggle source
# File lib/ork/model/document.rb, line 151
def __robject
  @__robject ||= model.bucket.new
end
__update_indices() click to toggle source

Build the secondary indices of this object

# File lib/ork/model/document.rb, line 132
def __update_indices
  model.indices.values.each do |index|
    __robject.indexes[index.riak_name] = index.value_from(attributes)
  end
end
load!(id) click to toggle source

Overwrite attributes with the persisted attributes in Riak.

# File lib/ork/model/document.rb, line 102
def load!(id)
  self.__robject.key = id
  __load_robject! id, @__robject.reload(force: true)
end