Class: Recliner::Document
Direct Known Subclasses
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
The Recliner::Database object used by the current instance.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compares documents for equality.
-
#delete ⇒ Object
Deletes the document in the database and marks the instance as read-only to reflect that no changes should be made (since they can’t be persisted).
-
#destroy ⇒ Object
Deletes the document in the database and marks the instance as read-only to reflect that no changes should be made (since they can’t be persisted).
-
#initialize(attributes = {}) {|_self| ... } ⇒ Document
constructor
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated properties).
-
#new_record? ⇒ Boolean
Returns true if this object hasn’t been saved yet – that is, a record for the object doesn’t exist yet; otherwise, returns false.
-
#read_only! ⇒ Object
Marks this document as read only.
-
#read_only? ⇒ Boolean
Returns true if this document has been marked as read only; otherwise, returns false.
-
#save ⇒ Object
:call-seq: save(perform_validation = true).
-
#save! ⇒ Object
Saves the model.
-
#update_attributes(attrs) ⇒ Object
Updates all the attributes from the passed-in Hash and saves the document.
Constructor Details
#initialize(attributes = {}) {|_self| ... } ⇒ Document
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated properties).
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/recliner/document.rb', line 10 def initialize(attributes={}) self.attributes = attributes @database = self.class.database @new_record = true yield self if block_given? _run_initialize_callbacks end |
Instance Attribute Details
#database ⇒ Object (readonly)
The Recliner::Database object used by the current instance
6 7 8 |
# File 'lib/recliner/document.rb', line 6 def database @database end |
Instance Method Details
#==(other) ⇒ Object
Compares documents for equality. Two documents are considered equal if they share the same document id and class.
108 109 110 |
# File 'lib/recliner/document.rb', line 108 def ==(other) other.class == self.class && other.id == self.id end |
#delete ⇒ Object
Deletes the document in the database and marks the instance as read-only to reflect that no changes should be made (since they can’t be persisted). Returns the deleted instance.
To enforce the object’s before_destroy
and after_destroy
callbacks, Observer methods, or any :dependent
association options, use #destroy
.
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/recliner/document.rb', line 74 def delete begin database.delete("#{id}?rev=#{rev}") unless new_record? rescue DocumentNotFound # OK - document is already deleted end read_only! self end |
#destroy ⇒ Object
Deletes the document in the database and marks the instance as read-only to reflect that no changes should be made (since they can’t be persisted).
88 89 90 |
# File 'lib/recliner/document.rb', line 88 def destroy delete end |
#new_record? ⇒ Boolean
Returns true if this object hasn’t been saved yet – that is, a record for the object doesn’t exist yet; otherwise, returns false.
93 94 95 |
# File 'lib/recliner/document.rb', line 93 def new_record? @new_record || false end |
#read_only! ⇒ Object
Marks this document as read only.
98 99 100 |
# File 'lib/recliner/document.rb', line 98 def read_only! attributes.freeze end |
#read_only? ⇒ Boolean
Returns true if this document has been marked as read only; otherwise, returns false.
103 104 105 |
# File 'lib/recliner/document.rb', line 103 def read_only? attributes.frozen? end |
#save ⇒ Object
:call-seq:
save(perform_validation = true)
Saves the document instance.
If the document is new a document gets created in the database, otherwise the existing document gets updated.
If perform_validation
is true validations run. If any of them fail the action is cancelled and save
returns false
. If the flag is false validations are bypassed altogether. See Recliner::Validations for more information.
There’s a series of callbacks associated with save
. If any of the before_*
callbacks return false
the action is cancelled and save
returns false
. See Recliner::Callbacks for further details.
38 39 40 41 42 |
# File 'lib/recliner/document.rb', line 38 def save create_or_update rescue DocumentNotSaved false end |
#save! ⇒ Object
Saves the model.
If the model is new a document gets created in the database, otherwise the existing document gets updated.
With save!
validations always run. If any of them fail Recliner::DocumentInvalid gets raised. See Recliner::Validations for more information.
There’s a series of callbacks associated with save!
. If any of the before_*
callbacks return false
the action is cancelled and save!
raises Recliner::DocumentNotSaved. See Recliner::Callbacks for further details.
57 58 59 |
# File 'lib/recliner/document.rb', line 57 def save! create_or_update || raise(DocumentNotSaved) end |
#update_attributes(attrs) ⇒ Object
Updates all the attributes from the passed-in Hash and saves the document. If the object is invalid, the saving will fail and false will be returned.
63 64 65 |
# File 'lib/recliner/document.rb', line 63 def update_attributes(attrs) self.attributes = attrs and save end |