Module: Ripple::Document

Extended by:
ActiveSupport::Concern
Defined in:
lib/ripple/document.rb,
lib/ripple/document/key.rb,
lib/ripple/document/link.rb,
lib/ripple/document/finders.rb,
lib/ripple/document/persistence.rb,
lib/ripple/document/bucket_access.rb

Overview

Represents a model stored in Riak, serialized in JSON object (document). Ripple::Document models aim to be fully ActiveModel compatible, with a keen eye toward features that developers expect from ActiveRecord, DataMapper and MongoMapper.

Example:

class Email
  include Ripple::Document
  property :from,    String, :presence => true
  property :to,      String, :presence => true
  property :sent,    Time,   :default => proc { Time.now }
  property :body,    String
end

email = Email.find("37458abc752f8413e")  # GET /riak/emails/37458abc752f8413e
email.from = "[email protected]"
email.save                               # PUT /riak/emails/37458abc752f8413e

reply = Email.new
reply.from = "[email protected]"
reply.to   = "[email protected]"
reply.body = "Riak is a good fit for scalable Ruby apps."
reply.save                               # POST /riak/emails (Riak-assigned key)

Defined Under Namespace

Modules: BucketAccess, ClassMethods, Finders, Key, Persistence Classes: Link

Instance Method Summary collapse

Instance Method Details

#==(comparison_object) ⇒ Object

Returns true if the comparison_object is the same object, or is of the same type and has the same key.



83
84
85
86
87
# File 'lib/ripple/document.rb', line 83

def ==(comparison_object)
  comparison_object.equal?(self) ||
    (comparison_object.class < Document && (comparison_object.instance_of?(self.class) || comparison_object.class.bucket.name == self.class.bucket.name) &&
     !new? && comparison_object.key == key && !comparison_object.new?)
end

#_root_documentObject



78
79
80
# File 'lib/ripple/document.rb', line 78

def _root_document
  self
end

#eql?(other) ⇒ Boolean

Returns:



89
90
91
92
93
94
95
# File 'lib/ripple/document.rb', line 89

def eql?(other)
  return true if other.equal?(self)

  (other.class.equal?(self.class)) &&
  !other.new? && !new? &&
  (other.key == key)
end

#hashObject



97
98
99
100
101
102
103
# File 'lib/ripple/document.rb', line 97

def hash
  if new?
    super # every new document should be treated as a different doc
  else
    [self.class, key].hash
  end
end


25
26
27
# File 'lib/ripple/document/link.rb', line 25

def to_link(tag)
  Link.new(self, tag)
end