Class: CouchRest::Model::Base
- Inherits:
-
Document
- Object
- Document
- CouchRest::Model::Base
- Extended by:
- ActiveModel::Naming
- Includes:
- Associations, Callbacks, CastedBy, ClassProxy, Collection, Configuration, Connection, DesignDoc, Designs, Dirty, DocumentQueries, ExtendedAttachments, Persistence, PropertyProtection, Proxyable, Validations, Views
- Defined in:
- lib/couchrest/model/base.rb
Constant Summary
Constants included from Callbacks
Class Method Summary collapse
- .inherited(subklass) ⇒ Object
-
.method_missing(m, *args, &block) ⇒ Object
Temp solution to make the view_by methods available.
- .subclasses ⇒ Object
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.
- #to_key ⇒ Object
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 Collection
Methods included from Proxyable
Methods included from ClassProxy
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.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/couchrest/model/base.rb', line 55 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.to_s end yield self if block_given? after_initialize if respond_to?(:after_initialize) run_callbacks(:initialize) { self } end |
Class Method Details
.inherited(subklass) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/couchrest/model/base.rb', line 30 def self.inherited(subklass) super subklass.send(:include, CouchRest::Model::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 |
.method_missing(m, *args, &block) ⇒ Object
Temp solution to make the view_by methods available
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/couchrest/model/base.rb', line 72 def self.method_missing(m, *args, &block) if has_view?(m) query = args.shift || {} return view(m, query, *args, &block) elsif m.to_s =~ /^find_(by_.+)/ view_name = $1 if has_view?(view_name) return first_from_view(view_name, *args) end end super end |
.subclasses ⇒ Object
26 27 28 |
# File 'lib/couchrest/model/base.rb', line 26 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.
101 102 103 104 105 106 107 108 109 |
# File 'lib/couchrest/model/base.rb', line 101 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 |
#to_key ⇒ Object
85 86 87 |
# File 'lib/couchrest/model/base.rb', line 85 def to_key new? ? nil : [id] end |