Class: CouchRest::Model::Base

Inherits:
Document
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
Associations, Callbacks, ClassProxy, Collection, Configuration, DesignDoc, DocumentQueries, ExtendedAttachments, Persistence, PropertyProtection, Validations, Views
Defined in:
lib/couchrest/model/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 ClassProxy

included

Methods included from ExtendedAttachments

#attachment_uri, #attachment_url, #attachments, #create_attachment, #delete_attachment, #has_attachment?, #read_attachment, #update_attachment

Methods included from DocumentQueries

included

Methods included from Callbacks

#valid?

Methods included from Persistence

#create, #create!, #destroy, #save, #save!, #update, #update_attributes

Constructor Details

#initialize(doc = {}, options = {}) ⇒ 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



49
50
51
52
53
54
55
56
# File 'lib/couchrest/model/base.rb', line 49

def initialize(doc = {}, options = {})
  doc = prepare_all_attributes(doc, options)
  super(doc)
  unless self['_id'] && self['_rev']
    self[self.model_type_key] = self.class.to_s
  end
  after_initialize if respond_to?(:after_initialize)
end

Instance Attribute Details

#casted_byObject

Accessors



39
40
41
# File 'lib/couchrest/model/base.rb', line 39

def casted_by
  @casted_by
end

Class Method Details

.inherited(subklass) ⇒ Object



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

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



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/couchrest/model/base.rb', line 60

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

.subclassesObject



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

def self.subclasses
  @subclasses ||= []
end

Instance Method Details

#base_docObject

Gets a reference to the actual document in the DB Calls up to the next document if there is one, Otherwise we’re at the top and we return self



78
79
80
81
# File 'lib/couchrest/model/base.rb', line 78

def base_doc
  return self if base_doc?
  @casted_by.base_doc
end

#base_doc?Boolean

Checks if we’re the top document

Returns:

  • (Boolean)


84
85
86
# File 'lib/couchrest/model/base.rb', line 84

def base_doc?
  !@casted_by
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

Hack so that CouchRest::Document, which descends from Hash, doesn’t appear to Rails routing as a Hash of options

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/couchrest/model/base.rb', line 92

def is_a?(klass)
  return false if klass == Hash
  super
end

#persisted?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/couchrest/model/base.rb', line 98

def persisted?
  !new?
end

#to_keyObject



102
103
104
# File 'lib/couchrest/model/base.rb', line 102

def to_key
  new? ? nil : [id] 
end