Class: CouchRest::Model::Base

Inherits:
Document
  • Object
show all
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

Callbacks::CALLBACKS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Translation

i18n_scope, lookup_ancestors

Methods included from Dirty

#couchrest_attribute_will_change!, #couchrest_parent_will_change!, #use_dirty?

Methods included from CastedBy

#base_doc, #base_doc?

Methods included from Validations

#valid?

Methods included from Associations

included

Methods included from PropertyProtection

#accessible_properties, included, #protected_properties, #remove_protected_attributes

Methods included from Proxyable

#proxy_database

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

#server

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.

Yields:

  • (_self)

Yield Parameters:



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 = {}, options = {})
  super()
  prepare_all_attributes(attributes, options)
  # set the instance's database, if provided
  self.database = options[:database] unless options[: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

.subclassesObject



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