Module: CouchRest::Model::Persistence::ClassMethods
- Defined in:
- lib/couchrest/model/persistence.rb
Instance Method Summary collapse
-
#build_from_database(doc = {}, options = {}, &block) ⇒ Object
Creates a new instance, bypassing attribute protection and uses the type field to determine which model to use to instanatiate the new object.
-
#create(attributes = {}, &block) ⇒ Object
Defines an instance and save it directly to the database.
-
#create!(attributes = {}, &block) ⇒ Object
Defines an instance and save it directly to the database.
-
#fail_validate!(document) ⇒ Object
Raise an error if validation failed.
-
#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 Details
#build_from_database(doc = {}, options = {}, &block) ⇒ Object
Creates a new instance, bypassing attribute protection and uses the type field to determine which model to use to instanatiate the new object.
Returns
a document instance
121 122 123 124 125 |
# File 'lib/couchrest/model/persistence.rb', line 121 def build_from_database(doc = {}, = {}, &block) src = doc[model_type_key] base = (src.blank? || src == self.to_s) ? self : src.constantize base.new(doc, .merge(:directly_set_attributes => true), &block) end |
#create(attributes = {}, &block) ⇒ Object
Defines an instance and save it directly to the database
Returns
returns the reloaded document
131 132 133 134 135 |
# File 'lib/couchrest/model/persistence.rb', line 131 def create(attributes = {}, &block) instance = new(attributes, &block) instance.create instance end |
#create!(attributes = {}, &block) ⇒ Object
Defines an instance and save it directly to the database
Returns
returns the reloaded document or raises an exception
141 142 143 144 145 |
# File 'lib/couchrest/model/persistence.rb', line 141 def create!(attributes = {}, &block) instance = new(attributes, &block) instance.create! instance end |
#fail_validate!(document) ⇒ Object
Raise an error if validation failed.
170 171 172 |
# File 'lib/couchrest/model/persistence.rb', line 170 def fail_validate!(document) raise Errors::Validations.new(document) 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.
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/couchrest/model/persistence.rb', line 155 def 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 |