Class: CouchRest::Model::Base

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

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 Collection

included

Methods included from Proxyable

#proxy_database

Methods included from ClassProxy

included

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:



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 = {}, 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



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

.respond_to?(m, include_private = false) ⇒ Boolean

compatbility for 1.8, it does not use respond_to_missing? thing is, when using it like this only, doing method(:find_by_view) will throw an error

Returns:

  • (Boolean)


88
89
90
# File 'lib/couchrest/model/base.rb', line 88

def self.respond_to?(m, include_private = false)
  super || respond_to_missing?(m, include_private)
end

.respond_to_missing?(m, include_private = false) ⇒ Boolean

ruby 1.9 feature this allows ruby to know that the method is defined using method_missing, and as such, method(:find_by_view) will actually give a Method back, and not throw an error like in 1.8!

Returns:

  • (Boolean)


96
97
98
# File 'lib/couchrest/model/base.rb', line 96

def self.respond_to_missing?(m, include_private = false)
  has_view?(m) || has_view?(m.to_s[/^find_(by_.+)/, 1])
end

.subclassesObject



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.



116
117
118
119
120
121
122
123
124
# File 'lib/couchrest/model/base.rb', line 116

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



100
101
102
# File 'lib/couchrest/model/base.rb', line 100

def to_key
  new? ? nil : [id]
end