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.
-
#model_type_value ⇒ Object
The value to use for this model’s model_type_key.
-
#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 == model_type_value) ? 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.
177 178 179 |
# File 'lib/couchrest/model/persistence.rb', line 177 def fail_validate!(document) raise Errors::Validations.new(document) end |
#model_type_value ⇒ Object
The value to use for this model’s model_type_key. By default, this shouls always be the string representation of the class, but if you need anything special, overwrite this method.
172 173 174 |
# File 'lib/couchrest/model/persistence.rb', line 172 def model_type_value to_s 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 |