Module: Restorm::Model::ORM
- Extended by:
- ActiveSupport::Concern
- Included in:
- Restorm::Model
- Defined in:
- lib/restorm/model/orm.rb
Overview
This module adds ORM-like capabilities to the model
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#decrement(attribute, by = 1) ⇒ Object
Initializes
attribute
to zero ifnil
and substracts the value passed asby
(default is 1). -
#decrement!(attribute, by = 1) ⇒ Object
Wrapper around #decrement that saves the resource.
-
#destroy(params = {}) ⇒ Object
Destroy a resource.
-
#destroyed? ⇒ Boolean
Return whether the object has been destroyed.
-
#increment(attribute, by = 1) ⇒ Object
Initializes
attribute
to zero ifnil
and adds the value passed asby
(default is 1). -
#increment!(attribute, by = 1) ⇒ Object
Wrapper around #increment that saves the resource.
-
#new? ⇒ Boolean
(also: #new_record?)
Return ‘true` if a resource was not saved yet.
-
#persisted? ⇒ Boolean
Return ‘true` if a resource is not `#new?`.
-
#reload(options = nil) ⇒ Object
Refetches the resource.
-
#save ⇒ Object
Save a resource and return ‘false` if the response is not a successful one or if there are errors in the resource.
-
#save! ⇒ Object
Similar to save(), except that ResourceInvalid is raised if the save fails.
-
#toggle(attribute) ⇒ Object
Assigns to
attribute
the boolean opposite ofattribute?
. -
#toggle!(attribute) ⇒ Object
Wrapper around #toggle that saves the resource.
-
#update_attributes(attributes) ⇒ Object
Update a resource and return it.
Instance Method Details
#decrement(attribute, by = 1) ⇒ Object
Initializes attribute
to zero if nil
and substracts the value passed as by
(default is 1). The decrement is performed directly on the underlying attribute, no setter is invoked. Only makes sense for number-based attributes. Returns self
.
112 113 114 |
# File 'lib/restorm/model/orm.rb', line 112 def decrement(attribute, by = 1) increment(attribute, -by) end |
#decrement!(attribute, by = 1) ⇒ Object
Wrapper around #decrement that saves the resource. Saving is subjected to validation checks. Returns self
.
118 119 120 |
# File 'lib/restorm/model/orm.rb', line 118 def decrement!(attribute, by = 1) increment!(attribute, -by) end |
#destroy(params = {}) ⇒ Object
Destroy a resource
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/restorm/model/orm.rb', line 80 def destroy(params = {}) method = self.class.method_for(:destroy) run_callbacks :destroy do self.class.request(params.merge(:_method => method, :_path => request_path)) do |parsed_data, response| load_from_parsed_data(parsed_data) @destroyed = response.success? end end self end |
#destroyed? ⇒ Boolean
Return whether the object has been destroyed
21 22 23 |
# File 'lib/restorm/model/orm.rb', line 21 def destroyed? @destroyed == true end |
#increment(attribute, by = 1) ⇒ Object
Initializes attribute
to zero if nil
and adds the value passed as by
(default is 1). The increment is performed directly on the underlying attribute, no setter is invoked. Only makes sense for number-based attributes. Returns self
.
95 96 97 98 99 |
# File 'lib/restorm/model/orm.rb', line 95 def increment(attribute, by = 1) attributes[attribute] ||= 0 attributes[attribute] += by self end |
#increment!(attribute, by = 1) ⇒ Object
Wrapper around #increment that saves the resource. Saving is subjected to validation checks. Returns self
.
103 104 105 106 |
# File 'lib/restorm/model/orm.rb', line 103 def increment!(attribute, by = 1) increment(attribute, by) && save self end |
#new? ⇒ Boolean Also known as: new_record?
Return ‘true` if a resource was not saved yet
10 11 12 |
# File 'lib/restorm/model/orm.rb', line 10 def new? id.nil? end |
#persisted? ⇒ Boolean
Return ‘true` if a resource is not `#new?`
16 17 18 |
# File 'lib/restorm/model/orm.rb', line 16 def persisted? !new? end |
#reload(options = nil) ⇒ Object
Refetches the resource
This method finds the resource by its primary key (which could be assigned manually) and modifies the object in-place.
154 155 156 157 158 |
# File 'lib/restorm/model/orm.rb', line 154 def reload( = nil) fresh_object = self.class.find(id) assign_attributes(fresh_object.attributes) self end |
#save ⇒ Object
Save a resource and return ‘false` if the response is not a successful one or if there are errors in the resource. Otherwise, return the newly updated resource
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/restorm/model/orm.rb', line 39 def save callback = new? ? :create : :update method = self.class.method_for(callback) run_callbacks :save do run_callbacks callback do self.class.request(to_params.merge(:_method => method, :_path => request_path)) do |parsed_data, response| load_from_parsed_data(parsed_data) return false if !response.success? || @response_errors.any? changes_applied end end end self end |
#save! ⇒ Object
Similar to save(), except that ResourceInvalid is raised if the save fails
57 58 59 60 61 62 |
# File 'lib/restorm/model/orm.rb', line 57 def save! unless save raise Restorm::Errors::ResourceInvalid, self end self end |
#toggle(attribute) ⇒ Object
Assigns to attribute
the boolean opposite of attribute?
. So if the predicate returns true
the attribute will become false
. This method toggles directly the underlying value without calling any setter. Returns self
.
132 133 134 135 |
# File 'lib/restorm/model/orm.rb', line 132 def toggle(attribute) attributes[attribute] = !public_send("#{attribute}?") self end |
#toggle!(attribute) ⇒ Object
Wrapper around #toggle that saves the resource. Saving is subjected to validation checks. Returns true
if the record could be saved.
139 140 141 |
# File 'lib/restorm/model/orm.rb', line 139 def toggle!(attribute) toggle(attribute) && save end |
#update_attributes(attributes) ⇒ Object
Update a resource and return it
70 71 72 |
# File 'lib/restorm/model/orm.rb', line 70 def update_attributes(attributes) assign_attributes(attributes) && save end |