Class: CouchRest::ExtendedDocument
- Includes:
- Callbacks, Mixins::ClassProxy, Mixins::Collection, Mixins::DesignDoc, Mixins::DocumentQueries, Mixins::ExtendedAttachments, Mixins::Views
- Defined in:
- lib/couchrest/more/extended_document.rb
Overview
Same as CouchRest::Document but with properties and validations
Instance Attribute Summary collapse
-
#casted_by ⇒ Object
Accessors.
Attributes inherited from Document
Class Method Summary collapse
-
.create(options) ⇒ Object
Defines an instance and save it directly to the database .
-
.create!(options) ⇒ Object
Defines an instance and save it directly to the database .
- .inherited(subklass) ⇒ Object
-
.method_missing(m, *args, &block) ⇒ Object
Temp solution to make the view_by methods available.
- .subclasses ⇒ Object
-
.timestamps! ⇒ Object
Automatically set
updated_at
andcreated_at
fields on the document whenever saving occurs. -
.unique_id(method = nil, &block) ⇒ Object
Name a method that will be called before the document is first saved, which returns a string to be used for the document’s
_id
.
Instance Method Summary collapse
-
#create(bulk = false) ⇒ Object
Trigger the callbacks (before, after, around) and create the document It’s important to have a create callback since you can’t check if a document was new after you saved it.
-
#create! ⇒ Object
Creates the document in the db.
-
#create_without_callbacks(bulk = false) ⇒ Object
unlike save, create returns the newly created document.
-
#destroy(bulk = false) ⇒ Object
Deletes the document from the database.
-
#initialize(passed_keys = {}) ⇒ ExtendedDocument
constructor
A new instance of ExtendedDocument.
-
#properties ⇒ Object
Returns the Class properties.
-
#save(bulk = false) ⇒ Object
Trigger the callbacks (before, after, around) and save the document.
-
#save! ⇒ Object
Saves the document to the db using save.
-
#save_without_callbacks(bulk = false) ⇒ Object
Overridden to set the unique ID.
-
#update(bulk = false) ⇒ Object
Trigger the callbacks (before, after, around) only if the document isn’t new.
-
#update_attributes(hash) ⇒ Object
Takes a hash as argument, and applies the values by using writer methods for each key.
-
#update_attributes_without_saving(hash) ⇒ Object
Takes a hash as argument, and applies the values by using writer methods for each key.
Methods included from Mixins::Collection
Methods included from Mixins::ClassProxy
Methods included from Mixins::ExtendedAttachments
#attachment_uri, #attachment_url, #create_attachment, #delete_attachment, #has_attachment?, #read_attachment, #update_attachment
Methods included from Mixins::DesignDoc
Methods included from Mixins::Views
Methods included from Mixins::DocumentQueries
Methods included from Callbacks
included, #method_missing, #run_callbacks
Methods inherited from Document
#copy, #id, #new_document?, #rev, #uri, use_database
Methods included from Mixins::Attachments
#delete_attachment, #fetch_attachment, #put_attachment
Methods inherited from Response
Methods inherited from Hash
Constructor Details
#initialize(passed_keys = {}) ⇒ ExtendedDocument
Returns a new instance of ExtendedDocument.
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/couchrest/more/extended_document.rb', line 41 def initialize(passed_keys={}) apply_defaults # defined in CouchRest::Mixins::Properties passed_keys.each do |k,v| if self.respond_to?("#{k}=") self.send("#{k}=", passed_keys.delete(k)) end end if passed_keys super cast_keys # defined in CouchRest::Mixins::Properties unless self['_id'] && self['_rev'] self['couchrest-type'] = self.class.to_s end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class CouchRest::Callbacks
Instance Attribute Details
#casted_by ⇒ Object
Accessors
33 34 35 |
# File 'lib/couchrest/more/extended_document.rb', line 33 def casted_by @casted_by end |
Class Method Details
.create(options) ⇒ Object
Defines an instance and save it directly to the database
Returns
returns the reloaded document
59 60 61 62 63 |
# File 'lib/couchrest/more/extended_document.rb', line 59 def self.create() instance = new() instance.create instance end |
.create!(options) ⇒ Object
Defines an instance and save it directly to the database
Returns
returns the reloaded document or raises an exception
69 70 71 72 73 |
# File 'lib/couchrest/more/extended_document.rb', line 69 def self.create!() instance = new() instance.create! instance end |
.inherited(subklass) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/couchrest/more/extended_document.rb', line 22 def self.inherited(subklass) subklass.send(:include, CouchRest::Mixins::Properties) subklass.class_eval <<-EOS, __FILE__, __LINE__ def self.inherited(subklass) subklass.properties = self.properties.dup end EOS subclasses << subklass end |
.method_missing(m, *args, &block) ⇒ Object
Temp solution to make the view_by methods available
112 113 114 115 116 117 118 119 |
# File 'lib/couchrest/more/extended_document.rb', line 112 def self.method_missing(m, *args, &block) if has_view?(m) query = args.shift || {} view(m, query, *args, &block) else super end end |
.subclasses ⇒ Object
18 19 20 |
# File 'lib/couchrest/more/extended_document.rb', line 18 def self.subclasses @subclasses ||= [] end |
.timestamps! ⇒ Object
Automatically set updated_at
and created_at
fields on the document whenever saving occurs. CouchRest uses a pretty decent time format by default. See Time#to_json
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/couchrest/more/extended_document.rb', line 78 def self. class_eval <<-EOS, __FILE__, __LINE__ property(:updated_at, :read_only => true, :cast_as => 'Time', :auto_validation => false) property(:created_at, :read_only => true, :cast_as => 'Time', :auto_validation => false) save_callback :before do |object| object['updated_at'] = Time.now object['created_at'] = object['updated_at'] if object.new_document? end EOS end |
.unique_id(method = nil, &block) ⇒ Object
Name a method that will be called before the document is first saved, which returns a string to be used for the document’s _id
. Because CouchDB enforces a constraint that each id must be unique, this can be used to enforce eg: uniq usernames. Note that this id must be globally unique across all document types which share a database, so if you’d like to scope uniqueness to this class, you should use the class name as part of the unique id.
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/couchrest/more/extended_document.rb', line 97 def self.unique_id method = nil, &block if method define_method :set_unique_id do self['_id'] ||= self.send(method) end elsif block define_method :set_unique_id do uniqid = block.call(self) raise ArgumentError, "unique_id block must not return nil" if uniqid.nil? self['_id'] ||= uniqid end end end |
Instance Method Details
#create(bulk = false) ⇒ Object
Trigger the callbacks (before, after, around) and create the document It’s important to have a create callback since you can’t check if a document was new after you saved it
When creating a document, both the create and the save callbacks will be triggered.
160 161 162 163 164 165 166 167 168 |
# File 'lib/couchrest/more/extended_document.rb', line 160 def create(bulk = false) caught = catch(:halt) do _run_create_callbacks do _run_save_callbacks do create_without_callbacks(bulk) end end end end |
#create! ⇒ Object
Creates the document in the db. Raises an exception if the document is not created properly.
180 181 182 |
# File 'lib/couchrest/more/extended_document.rb', line 180 def create! raise "#{self.inspect} failed to save" unless self.create end |
#create_without_callbacks(bulk = false) ⇒ Object
unlike save, create returns the newly created document
171 172 173 174 175 176 |
# File 'lib/couchrest/more/extended_document.rb', line 171 def create_without_callbacks(bulk =false) raise ArgumentError, "a document requires a database to be created to (The document or the #{self.class} default database were not set)" unless database set_unique_id if new_document? && self.respond_to?(:set_unique_id) result = database.save_doc(self, bulk) (result["ok"] == true) ? self : false end |
#destroy(bulk = false) ⇒ Object
Deletes the document from the database. Runs the :destroy callbacks. Removes the _id
and _rev
fields, preparing the document to be saved to a new _id
.
232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/couchrest/more/extended_document.rb', line 232 def destroy(bulk=false) caught = catch(:halt) do _run_destroy_callbacks do result = database.delete_doc(self, bulk) if result['ok'] self.delete('_rev') self.delete('_id') end result['ok'] end end end |
#properties ⇒ Object
Returns the Class properties
Returns
- Array
-
the list of properties for the instance
127 128 129 |
# File 'lib/couchrest/more/extended_document.rb', line 127 def properties self.class.properties end |
#save(bulk = false) ⇒ Object
Trigger the callbacks (before, after, around) and save the document
202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/couchrest/more/extended_document.rb', line 202 def save(bulk = false) caught = catch(:halt) do if self.new_document? _run_save_callbacks do save_without_callbacks(bulk) end else update(bulk) end end end |
#save! ⇒ Object
Saves the document to the db using save. Raises an exception if the document is not saved properly.
225 226 227 |
# File 'lib/couchrest/more/extended_document.rb', line 225 def save! raise "#{self.inspect} failed to save" unless self.save end |
#save_without_callbacks(bulk = false) ⇒ Object
Overridden to set the unique ID. Returns a boolean value
216 217 218 219 220 221 |
# File 'lib/couchrest/more/extended_document.rb', line 216 def save_without_callbacks(bulk = false) raise ArgumentError, "a document requires a database to be saved to (The document or the #{self.class} default database were not set)" unless database set_unique_id if new_document? && self.respond_to?(:set_unique_id) result = database.save_doc(self, bulk) result["ok"] == true end |
#update(bulk = false) ⇒ Object
Trigger the callbacks (before, after, around) only if the document isn’t new
186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/couchrest/more/extended_document.rb', line 186 def update(bulk = false) caught = catch(:halt) do if self.new_document? save(bulk) else _run_update_callbacks do _run_save_callbacks do save_without_callbacks(bulk) end end end end end |
#update_attributes(hash) ⇒ Object
Takes a hash as argument, and applies the values by using writer methods for each key. Raises a NoMethodError if the corresponding methods are missing. In case of error, no attributes are changed.
146 147 148 149 |
# File 'lib/couchrest/more/extended_document.rb', line 146 def update_attributes(hash) update_attributes_without_saving hash save end |
#update_attributes_without_saving(hash) ⇒ Object
Takes a hash as argument, and applies the values by using writer methods for each key. It doesn’t save the document at the end. Raises a NoMethodError if the corresponding methods are missing. In case of error, no attributes are changed.
134 135 136 137 138 139 140 141 |
# File 'lib/couchrest/more/extended_document.rb', line 134 def update_attributes_without_saving(hash) hash.each do |k, v| raise NoMethodError, "#{k}= method not available, use property :#{k}" unless self.respond_to?("#{k}=") end hash.each do |k, v| self.send("#{k}=",v) end end |