Class: LooseChange::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, Attachments, Attributes, Helpers, Naming, Pagination, Persistence, Spatial, Views
Includes:
ActiveModel::AttributeMethods, ActiveModel::Dirty, ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Validations, AttachmentClassMethods, AttributeClassMethods, Errors, Helpers, NamingClassMethods, PersistenceClassMethods
Defined in:
lib/loose_change/base.rb

Constant Summary

Constants included from Spatial

Spatial::GEOMETRIES

Instance Attribute Summary collapse

Attributes included from PersistenceClassMethods

#_attachments, #_id, #_rev, #database, #destroyed, #id, #new_record

Attributes included from Errors

#errors

Instance Method Summary collapse

Methods included from Attributes

default, property, timestamps!

Methods included from Persistence

create, create!, find, instantiate_from_hash, use_database

Methods included from Views

add_view, all, doc_key, existence_check, key_for, view, view_by, view_by_all

Methods included from Pagination

paginate, paginated_by, paginated_view_by

Methods included from Spatial

by_bounding_box

Methods included from Helpers

default_headers, design_document_uri

Methods included from PersistenceClassMethods

#destroy, #destroyed?, #new_record?, #persisted?, #save, #save!

Methods included from NamingClassMethods

#model_name

Methods included from AttachmentClassMethods

#attach, #attachment, #put_attachment, #retrieve_attachment

Methods included from AttributeClassMethods

#attributes, #update_attribute, #update_attributes

Constructor Details

#initialize(args = {}) ⇒ Base

Returns a new instance of Base.



60
61
62
63
64
65
66
67
# File 'lib/loose_change/base.rb', line 60

def initialize(args = {})
  @errors = ActiveModel::Errors.new(self)
  @database = self.database
  @new_record = true unless args['_id']
  args.each {|property, value| self.send("#{property}=".to_sym, value)}
  apply_defaults
  self
end

Instance Attribute Details

#attachmentsObject

Returns the value of attribute attachments.



33
34
35
# File 'lib/loose_change/base.rb', line 33

def attachments
  @attachments
end

Instance Method Details

#==(other_model) ⇒ Object Also known as: eql?



47
48
49
50
51
52
# File 'lib/loose_change/base.rb', line 47

def ==(other_model)
  return false unless other_model && other_model.is_a?(self.class)
  id == other_model.id &&
    _rev == other_model._rev &&
    !(changed? || other_model.changed?)
end

#hashObject



56
57
58
# File 'lib/loose_change/base.rb', line 56

def hash
  id.try(:hex) || super
end

#to_keyObject



35
36
37
# File 'lib/loose_change/base.rb', line 35

def to_key
  persisted? ? [id] : nil
end

#to_modelObject



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

def to_model
  self
end

#to_paramObject



43
44
45
# File 'lib/loose_change/base.rb', line 43

def to_param
  (to_key && persisted?) ? to_key.join('-') : nil
end