Class: CouchModel::Base
- Inherits:
-
Object
show all
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming, ActiveModel::Translation
- Includes:
- ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, ActiveModel::Validations, Accessor, Association, Finder, Setup
- Defined in:
- lib/couch_model/base.rb,
lib/couch_model/base/setup.rb,
lib/couch_model/base/finder.rb,
lib/couch_model/active_model.rb,
lib/couch_model/base/accessor.rb,
lib/couch_model/base/association.rb
Overview
This should extend the Base class to provide association methods.
Defined Under Namespace
Modules: Accessor, Association, Finder, Setup
Classes: InvalidModelError, NotFoundError
Constant Summary
collapse
- CALLBACKS =
[ :initialize, :save, :create, :update, :destroy ].freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
included
Methods included from Finder
included
Methods included from Accessor
included
Methods included from Setup
included
Constructor Details
#initialize(attributes = { }) ⇒ Base
Returns a new instance of Base.
29
30
31
32
33
34
35
36
37
|
# File 'lib/couch_model/base.rb', line 29
def initialize(attributes = { })
klass = self.class
@attributes = { Configuration::CLASS_KEY => klass.to_s }
self.attributes = attributes
klass.key_definitions.each do |key, definition|
@attributes[key] = definition[:default] if definition.has_key?(:default) && !@attributes.has_key?(key)
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
27
28
29
|
# File 'lib/couch_model/base.rb', line 27
def attributes
@attributes
end
|
Class Method Details
.create!(*arguments) ⇒ Object
108
109
110
111
112
|
# File 'lib/couch_model/active_model.rb', line 108
def create!(*arguments)
model = new *arguments
model.save!
model
end
|
.key_accessor(key, options = { }) ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/couch_model/active_model.rb', line 100
def key_accessor(key, options = { })
add_key key
redefine_attribute_methods
key_accessor_without_dirty key, options
redefine_key_writer key
end
|
.key_accessor_without_dirty ⇒ Object
98
|
# File 'lib/couch_model/active_model.rb', line 98
alias key_accessor_without_dirty key_accessor
|
Instance Method Details
#==(other) ⇒ Object
58
59
60
|
# File 'lib/couch_model/base.rb', line 58
def ==(other)
self.id == other.id
end
|
#destroy ⇒ Object
79
80
81
82
83
84
85
86
|
# File 'lib/couch_model/base.rb', line 79
def destroy
return false if new?
Transport::JSON.request :delete, self.url, :headers => { "If-Match" => self.rev }, :expected_status_code => 200
clear_rev
true
rescue Transport::UnexpectedStatusCodeError => error
upgrade_unexpected_status_error error
end
|
#id ⇒ Object
Also known as:
_id
44
45
46
|
# File 'lib/couch_model/base.rb', line 44
def id
@attributes["_id"]
end
|
#id=(value) ⇒ Object
49
50
51
|
# File 'lib/couch_model/base.rb', line 49
def id=(value)
@attributes["_id"] = value
end
|
#load ⇒ Object
Also known as:
reload
#new? ⇒ Boolean
Also known as:
new_record?, destroyed?
62
63
64
|
# File 'lib/couch_model/base.rb', line 62
def new?
self.rev.nil?
end
|
#persisted? ⇒ Boolean
40
41
42
|
# File 'lib/couch_model/active_model.rb', line 40
def persisted?
!new?
end
|
#rev ⇒ Object
Also known as:
_rev
53
54
55
|
# File 'lib/couch_model/base.rb', line 53
def rev
@attributes["_rev"]
end
|
#save ⇒ Object
75
76
77
|
# File 'lib/couch_model/base.rb', line 75
def save
new? ? create : update
end
|
#save! ⇒ Object
59
60
61
62
63
|
# File 'lib/couch_model/active_model.rb', line 59
def save!
raise InvalidModelError, "errors: #{errors.full_messages.join(' / ')}" unless valid?
raise StandardError, "unknown error while saving model" unless save
true
end
|
#save_without_active_model ⇒ Object
50
51
52
|
# File 'lib/couch_model/active_model.rb', line 50
def save
new? ? create : update
end
|
#to_param ⇒ Object
46
47
48
|
# File 'lib/couch_model/active_model.rb', line 46
def to_param
persisted? ? id : nil
end
|
#update_attribute(name, value) ⇒ Object
65
66
67
|
# File 'lib/couch_model/active_model.rb', line 65
def update_attribute(name, value)
update_attributes name => value
end
|
#update_attributes(attributes) ⇒ Object
69
70
71
72
|
# File 'lib/couch_model/active_model.rb', line 69
def update_attributes(attributes)
self.attributes = attributes
self.save
end
|
#url ⇒ Object
88
89
90
|
# File 'lib/couch_model/base.rb', line 88
def url
"#{self.database.url}/#{self.id}"
end
|