Module: Ripple::EmbeddedDocument::Persistence

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

Overview

Adds methods to Ripple::EmbeddedDocument that delegate storage operations to the parent document.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_parent_documentObject

The parent document to this embedded document. This may be a Document or another Ripple::EmbeddedDocument.



29
30
31
# File 'lib/ripple/embedded_document/persistence.rb', line 29

def _parent_document
  @_parent_document
end

Instance Method Details

#_root_documentObject

The root Document to which this embedded document belongs.



70
71
72
# File 'lib/ripple/embedded_document/persistence.rb', line 70

def _root_document
  @_parent_document.try(:_root_document)
end

#attributes_for_persistenceObject



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

def attributes_for_persistence
  raw_attributes.merge("_type" => self.class.name)
end

#new?Boolean

Whether the root document is unsaved.

Returns:



32
33
34
35
36
37
38
# File 'lib/ripple/embedded_document/persistence.rb', line 32

def new?
  if _root_document
    _root_document.new?
  else
    true
  end
end

#save(*args) ⇒ Object

Saves this embedded document by delegating to the root document.



54
55
56
57
58
59
60
61
62
# File 'lib/ripple/embedded_document/persistence.rb', line 54

def save(*args)
  if _root_document
    run_save_callbacks do
      _root_document.save(*args)
    end
  else
    raise NoRootDocument.new(self, :save)
  end
end

#update_attribute(attribute, value) ⇒ Object

Updates this embedded document’s attribute and saves the root document, skipping validations.



48
49
50
51
# File 'lib/ripple/embedded_document/persistence.rb', line 48

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

#update_attributes(attrs) ⇒ Object

Sets this embedded documents attributes and saves the root document.



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

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