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.defaults.each do |key, value|
@attributes[key] = value unless @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
91
92
93
94
|
# File 'lib/couch_model/base.rb', line 91
def method_missing(method_name, *arguments, &block)
return @attributes[Configuration::CLASS_KEY] if Configuration::CLASS_KEY == method_name.to_s
super
end
|
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
84
85
86
87
88
|
# File 'lib/couch_model/active_model.rb', line 84
def create!(*arguments)
model = new *arguments
model.save!
model
end
|
.key_accessor(key, options = { }) ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/couch_model/active_model.rb', line 76
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
74
|
# File 'lib/couch_model/active_model.rb', line 74
alias key_accessor_without_dirty key_accessor
|
Instance Method Details
#==(other) ⇒ Object
57
58
59
|
# File 'lib/couch_model/base.rb', line 57
def ==(other)
self.id == other.id
end
|
#destroy ⇒ Object
78
79
80
81
82
83
84
85
|
# File 'lib/couch_model/base.rb', line 78
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
43
44
45
|
# File 'lib/couch_model/base.rb', line 43
def id
@attributes["_id"]
end
|
#id=(value) ⇒ Object
48
49
50
|
# File 'lib/couch_model/base.rb', line 48
def id=(value)
@attributes["_id"] = value
end
|
#load ⇒ Object
Also known as:
reload
65
66
67
68
69
70
|
# File 'lib/couch_model/base.rb', line 65
def load
load_response Transport::JSON.request(:get, url, :expected_status_code => 200)
true
rescue Transport::UnexpectedStatusCodeError => error
upgrade_unexpected_status_error error
end
|
#new? ⇒ Boolean
Also known as:
new_record?, destroyed?
61
62
63
|
# File 'lib/couch_model/base.rb', line 61
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
52
53
54
|
# File 'lib/couch_model/base.rb', line 52
def rev
@attributes["_rev"]
end
|
#save ⇒ Object
74
75
76
|
# File 'lib/couch_model/base.rb', line 74
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
|
#url ⇒ Object
87
88
89
|
# File 'lib/couch_model/base.rb', line 87
def url
"#{self.database.url}/#{self.id}"
end
|