Module: Her::Model::ORM
Overview
This module adds ORM-like capabilities to the model
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#destroy(params = {}) ⇒ Object
Destroy a resource.
-
#destroyed? ⇒ Boolean
Return whether the object has been destroyed.
-
#new? ⇒ Boolean
(also: #new_record?)
Return ‘true` if a resource was not saved yet.
-
#persisted? ⇒ Boolean
Return ‘true` if a resource is not `#new?`.
-
#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.
Instance Method Details
#destroy(params = {}) ⇒ Object
Destroy a resource
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/castle-her/model/orm.rb', line 76 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| assign_attributes(self.class.parse(parsed_data[:data])) if parsed_data[:data].any? @metadata = parsed_data[:metadata] @response_errors = parsed_data[:errors] @destroyed = true end end self end |
#destroyed? ⇒ Boolean
Return whether the object has been destroyed
20 21 22 |
# File 'lib/castle-her/model/orm.rb', line 20 def destroyed? @destroyed == true end |
#new? ⇒ Boolean Also known as: new_record?
Return ‘true` if a resource was not saved yet
9 10 11 |
# File 'lib/castle-her/model/orm.rb', line 9 def new? id.nil? end |
#persisted? ⇒ Boolean
Return ‘true` if a resource is not `#new?`
15 16 17 |
# File 'lib/castle-her/model/orm.rb', line 15 def persisted? !new? 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
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/castle-her/model/orm.rb', line 38 def save callback = new? ? :create : :update method = self.class.method_for(callback) run_callbacks callback do run_callbacks :save do params = to_params self.class.request(to_params.merge(:_method => method, :_path => request_path)) do |parsed_data, response| assign_attributes(self.class.parse(parsed_data[:data])) if parsed_data[:data].any? @metadata = parsed_data[:metadata] @response_errors = parsed_data[:errors] return false if !response.success? || @response_errors.any? if self.changed_attributes.present? @previously_changed = self.changed_attributes.clone self.changed_attributes.clear end end end end self end |
#save! ⇒ Object
Similar to save(), except that ResourceInvalid is raised if the save fails
63 64 65 66 67 68 |
# File 'lib/castle-her/model/orm.rb', line 63 def save! if !self.save raise Her::Errors::ResourceInvalid, self end self end |