Class: CouchRest::Model::Base

Inherits:
Document
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
Associations, Callbacks, CastedBy, Configuration, Connection, Designs, Dirty, DocumentQueries, ExtendedAttachments, Persistence, PropertyProtection, Proxyable, Validations
Defined in:
lib/couchrest/model/base.rb

Constant Summary

Constant Summary

Constants included from Callbacks

Callbacks::CALLBACKS

Class Method Summary collapse

Instance Method Summary collapse

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:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/couchrest/model/base.rb', line 50

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.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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/couchrest/model/base.rb', line 25

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

.subclassesObject



21
22
23
# File 'lib/couchrest/model/base.rb', line 21

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.



81
82
83
84
85
86
87
88
89
# File 'lib/couchrest/model/base.rb', line 81

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_keyObject



65
66
67
# File 'lib/couchrest/model/base.rb', line 65

def to_key
  new? ? nil : [id]
end