Module: Ripple::Document::Persistence

Extended by:
ActiveSupport::Concern
Defined in:
lib/ripple/document/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#robjectObject



112
113
114
115
116
# File 'lib/ripple/document/persistence.rb', line 112

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

Instance Method Details

#deleted?Boolean

Determines whether this document has been deleted or not.

Returns:



37
38
39
# File 'lib/ripple/document/persistence.rb', line 37

def deleted?
  @deleted
end

#destroyObject



92
93
94
95
96
97
# File 'lib/ripple/document/persistence.rb', line 92

def destroy
  destroy!
  true
rescue Riak::FailedRequest
  false
end

#destroy!Object

Deletes the document from Riak and freezes this instance



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

def destroy!
  robject.delete(self.class.quorums.slice(:rw)) unless new?
  @deleted = true
  freeze
end

#freezeObject

Freeze the attributes hash instead of the record itself to avoid errors when calling methods on frozen records.



101
102
103
# File 'lib/ripple/document/persistence.rb', line 101

def freeze
  @attributes.freeze
end

#frozen?Boolean

Returns true if the attributes hash has been frozen.

Returns:



106
107
108
# File 'lib/ripple/document/persistence.rb', line 106

def frozen?
  @attributes.frozen?
end

#initializeObject



30
31
32
33
34
# File 'lib/ripple/document/persistence.rb', line 30

def initialize
  super
  @new = true
  @deleted = false
end

#new?Boolean

Determines whether this is a new document.

Returns:



42
43
44
# File 'lib/ripple/document/persistence.rb', line 42

def new?
  @new || false
end

#really_save(*args) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/ripple/document/persistence.rb', line 67

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

#reloadObject

Reloads the document from Riak

Returns:

  • self



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

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

#save(*args) ⇒ true, false

Saves the document in Riak.

Returns:

  • (true, false)

    whether the document succeeded in saving



63
64
65
# File 'lib/ripple/document/persistence.rb', line 63

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



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

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



56
57
58
59
# File 'lib/ripple/document/persistence.rb', line 56

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

#update_robjectObject



118
119
120
121
122
# File 'lib/ripple/document/persistence.rb', line 118

def update_robject
  robject.key = key if robject.key != key
  robject.content_type = 'application/json'
  robject.data = attributes_for_persistence
end