module Ork::Model

Attributes

attributes[R]
embedding[R]

Public Class Methods

included(klass) click to toggle source
# File lib/ork/model.rb, line 8
def self.included(klass)
  klass.extend(Ork::Model::ClassMethods)
  klass.extend(Ork::Model::Associations)
end
new(atts = {}) click to toggle source

Initialize a model using a dictionary of attributes.

Example:

u = User.new(:name => "John")
# File lib/ork/model.rb, line 19
def initialize(atts = {})
  @attributes = {}
  @embedding = {}
  @_memo = {}
  update_attributes(model.defaults.merge(atts))
end

Public Instance Methods

update_attributes(atts) click to toggle source

Write the dictionary of key-value pairs to the model.

# File lib/ork/model.rb, line 28
def update_attributes(atts)
  atts.delete('_type')
  atts.each { |att, val| send(:"#{att}=", val) }
end
update_embedded_attributes(atts) click to toggle source

Writhe the dictionary of key-value pairs of embedded objects.

# File lib/ork/model.rb, line 35
def update_embedded_attributes(atts)
  atts.each do |att, val|
    @embedding[att] = val
  end
end

Protected Instance Methods

__persist_attributes() click to toggle source
# File lib/ork/model.rb, line 43
def __persist_attributes
  attributes = @attributes.merge('_type' => model.name)
  attributes.delete(model.__parent_key) if model.respond_to? :__parent_key

  model.embedding.each do |embedded|
    object = self.send(embedded)
    unless object.nil?
      attributes[embedded] = if object.is_a? Array
                               object.map{|o| o.send :__persist_attributes}
                             else
                               object.__persist_attributes
                             end
    end
  end

  attributes
end
assert_embeddable(object) click to toggle source
# File lib/ork/model.rb, line 77
def assert_embeddable(object)
  unless object.respond_to?(:embeddable?) && object.embeddable?
    raise Ork::NotEmbeddable.new(object)
  end
end
assert_valid_class(object, model) click to toggle source
# File lib/ork/model.rb, line 72
def assert_valid_class(object, model)
  raise Ork::NotOrkObject.new(object) unless object.class.include? Ork::Document
  raise Ork::InvalidClass.new(object) if object.class.name != model.to_s
end
model() click to toggle source
# File lib/ork/model.rb, line 61
def model
  self.class
end
new_embedded(model, attributes) click to toggle source
# File lib/ork/model.rb, line 65
def new_embedded(model, attributes)
  attributes[model.__parent_key] = self
  attributes.delete '_type'

  model.new attributes
end