Module: Ripple::Document::Persistence::InstanceMethods

Defined in:
lib/ripple/document/persistence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#robjectObject



110
111
112
113
114
# File 'lib/ripple/document/persistence.rb', line 110

def robject
  @robject ||= Riak::RObject.new(self.class.bucket, key).tap do |obj|
    obj.content_type = "application/json"
  end
end

Instance Method Details

#destroyObject

Deletes the document from Riak and freezes this instance



95
96
97
98
99
100
101
# File 'lib/ripple/document/persistence.rb', line 95

def destroy
  robject.delete(self.class.quorums.slice(:rw)) unless new?
  freeze
  true
rescue Riak::FailedRequest
  false
end

#freezeObject

Freezes the document, preventing further modification.



104
105
106
# File 'lib/ripple/document/persistence.rb', line 104

def freeze
  @attributes.freeze; super
end

#initializeObject



45
46
47
48
# File 'lib/ripple/document/persistence.rb', line 45

def initialize
  super
  @new = true
end

#new?Boolean

Determines whether this is a new document.

Returns:



51
52
53
# File 'lib/ripple/document/persistence.rb', line 51

def new?
  @new || false
end

#really_save(*args) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/ripple/document/persistence.rb', line 76

def really_save(*args)
  robject.key = key if robject.key != key
  robject.data = attributes_for_persistence
  robject.store(self.class.quorums.slice(:w,:dw))
  self.key = robject.key
  @new = false
  true
end

#reloadObject

Reloads the document from Riak

Returns:

  • self



87
88
89
90
91
92
# File 'lib/ripple/document/persistence.rb', line 87

def reload
  return self if new?
  robject.reload(:force => true)
  self.__send__(:attributes=, @robject.data.except("_type"), false)
  self
end

#save(*args) ⇒ true, false

Saves the document in Riak.

Returns:

  • (true, false)

    whether the document succeeded in saving



72
73
74
# File 'lib/ripple/document/persistence.rb', line 72

def save(*args)
  really_save(*args)
end

#update_attribute(attribute, value) ⇒ true, false

Updates a single attribute and then saves the document NOTE: THIS SKIPS VALIDATIONS! Use with caution.

Returns:

  • (true, false)

    whether the document succeeded in saving



58
59
60
61
# File 'lib/ripple/document/persistence.rb', line 58

def update_attribute(attribute, value)
  send("#{attribute}=", value)
  save(:validate => false)
end

#update_attributes(attrs) ⇒ true, false

Writes new attributes and then saves the document

Returns:

  • (true, false)

    whether the document succeeded in saving



65
66
67
68
# File 'lib/ripple/document/persistence.rb', line 65

def update_attributes(attrs)
  self.attributes = attrs
  save
end