Class: CouchRest::Model::Base
- Inherits:
-
Document
- Object
- Document
- CouchRest::Model::Base
- Extended by:
- Translation
- Includes:
- ActiveModel::Conversion, Associations, Callbacks, CastedBy, Configuration, Connection, Designs, Dirty, DocumentQueries, ExtendedAttachments, Persistence, PropertyProtection, Proxyable, Validations
- Defined in:
- lib/couchrest/model/base.rb
Constant Summary
Constants included from Callbacks
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Compare this model with another by confirming to see if the IDs and their databases match!.
-
#initialize(attributes = {}, options = {}) {|_self| ... } ⇒ Base
constructor
Instantiate a new CouchRest::Model::Base by preparing all properties using the provided document hash.
Methods included from Translation
Methods included from Dirty
#couchrest_attribute_will_change!, #couchrest_parent_will_change!, #use_dirty?
Methods included from CastedBy
Methods included from Validations
Methods included from Associations
Methods included from PropertyProtection
#accessible_properties, included, #protected_properties, #remove_protected_attributes
Methods included from Proxyable
Methods included from ExtendedAttachments
#attachment_uri, #attachment_url, #attachments, #create_attachment, #delete_attachment, #has_attachment?, #read_attachment, #update_attachment
Methods included from Persistence
#create, #create!, #destroy, #destroyed?, #persisted?, #reload, #save, #save!, #update, #update_attributes
Methods included from Connection
Constructor Details
#initialize(attributes = {}, options = {}) {|_self| ... } ⇒ Base
Instantiate a new CouchRest::Model::Base by preparing all properties using the provided document hash.
Options supported:
-
:directly_set_attributes, true when data comes directly from database
-
:database, provide an alternative database
If a block is provided the new model will be passed into the block so that it can be populated.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/couchrest/model/base.rb', line 53 def initialize(attributes = {}, = {}) super() prepare_all_attributes(attributes, ) # set the instance's database, if provided self.database = [:database] unless [:database].nil? unless self['_id'] && self['_rev'] self[self.model_type_key] = self.class.model_type_value end yield self if block_given? after_initialize if respond_to?(:after_initialize) run_callbacks(:initialize) { self } end |
Class Method Details
.inherited(subklass) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/couchrest/model/base.rb', line 28 def self.inherited(subklass) super subklass.send(:include, Properties) subklass.class_eval <<-EOS, __FILE__, __LINE__ + 1 def self.inherited(subklass) super subklass.properties = self.properties.dup # This is nasty: subklass._validators = self._validators.dup end EOS subclasses << subklass end |
.subclasses ⇒ Object
24 25 26 |
# File 'lib/couchrest/model/base.rb', line 24 def self.subclasses @subclasses ||= [] end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Compare this model with another by confirming to see if the IDs and their databases match!
Camparison of the database is required in case the model has been proxied or loaded elsewhere.
A Basic CouchRest document will only ever compare using a Hash comparison on the attributes.
79 80 81 82 83 84 85 86 87 |
# File 'lib/couchrest/model/base.rb', line 79 def == other return false unless other.is_a?(Base) if id.nil? && other.id.nil? # no ids? assume comparing nested and revert to hash comparison to_hash == other.to_hash else database == other.database && id == other.id end end |