Class: StorageRoom::Model
- Defined in:
- lib/storage_room/model.rb
Overview
Abstract superclass for classes that can persist to the remote servers
Direct Known Subclasses
Instance Attribute Summary collapse
-
#skip_webhooks ⇒ Object
Returns the value of attribute skip_webhooks.
Class Method Summary collapse
-
.all ⇒ Object
Get all models (could be paginated).
-
.create(attributes = {}) ⇒ Object
Create a new model with the passed attributes.
-
.find(id) ⇒ Object
Find a model with the specified id.
-
.index_path ⇒ Object
:nodoc:.
-
.json_name ⇒ Object
:nodoc:.
-
.show_path(id) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#create ⇒ Object
Create a new model on the server.
-
#destroy ⇒ Object
Delete an existing model on the server.
-
#errors ⇒ Object
The validation errors that were returned by the server.
-
#initialize(attributes = {}) ⇒ Model
constructor
Create a new model and set its attributes.
-
#new_record? ⇒ Boolean
Has this model been saved to the API already?.
-
#reset! ⇒ Object
Reset the model to its default state.
-
#save ⇒ Object
Create a new model or update an existing model on the server.
-
#to_hash(args = {}) ⇒ Object
ActiveSupport caused problems when using as_json, so using to_hash.
-
#update ⇒ Object
Update an existing model on the server.
-
#valid? ⇒ Boolean
Is the model valid or were there validation errors on the server?.
Methods inherited from Resource
handle_critical_response_errors, #loaded?, meta_data?, #reload
Methods included from Plugins
Methods included from Accessors
#[], #as_json, #attributes, #attributes=, #eql?, #hash, #inspect, #loaded?, #proxy?, #response_data, #response_data=, #set_from_response_data
Constructor Details
#initialize(attributes = {}) ⇒ Model
Create a new model and set its attributes
41 42 43 44 45 |
# File 'lib/storage_room/model.rb', line 41 def initialize(attributes={}) @errors = [] super end |
Instance Attribute Details
#skip_webhooks ⇒ Object
Returns the value of attribute skip_webhooks.
6 7 8 |
# File 'lib/storage_room/model.rb', line 6 def skip_webhooks @skip_webhooks end |
Class Method Details
.all ⇒ Object
Get all models (could be paginated)
17 18 19 |
# File 'lib/storage_room/model.rb', line 17 def all load(index_path) end |
.create(attributes = {}) ⇒ Object
Create a new model with the passed attributes
10 11 12 13 14 |
# File 'lib/storage_room/model.rb', line 10 def create(attributes={}) entry = new(attributes) entry.create entry end |
.find(id) ⇒ Object
Find a model with the specified id
22 23 24 |
# File 'lib/storage_room/model.rb', line 22 def find(id) load(show_path(id)) end |
.index_path ⇒ Object
:nodoc:
27 28 29 |
# File 'lib/storage_room/model.rb', line 27 def index_path # :nodoc: raise StorageRoom::AbstractMethodError.new end |
.json_name ⇒ Object
:nodoc:
35 36 37 |
# File 'lib/storage_room/model.rb', line 35 def json_name # :nodoc: raise StorageRoom::AbstractMethodError.new end |
.show_path(id) ⇒ Object
:nodoc:
31 32 33 |
# File 'lib/storage_room/model.rb', line 31 def show_path(id) # :nodoc: raise StorageRoom::AbstractMethodError.new end |
Instance Method Details
#create ⇒ Object
Create a new model on the server
65 66 67 68 69 70 71 72 73 |
# File 'lib/storage_room/model.rb', line 65 def create return false unless new_record? run_callbacks :save do run_callbacks :create do httparty = self.class.post(self.class.index_path, .merge(:body => to_json)) handle_save_response(httparty) end end end |
#destroy ⇒ Object
Delete an existing model on the server
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/storage_room/model.rb', line 87 def destroy return false if new_record? run_callbacks :destroy do httparty = self.class.delete(self[:@url], ) self.class.handle_critical_response_errors(httparty) end true end |
#errors ⇒ Object
The validation errors that were returned by the server
117 118 119 |
# File 'lib/storage_room/model.rb', line 117 def errors @errors ||= [] end |
#new_record? ⇒ Boolean
Has this model been saved to the API already?
55 56 57 |
# File 'lib/storage_room/model.rb', line 55 def new_record? self['@version'] ? false : true end |
#reset! ⇒ Object
Reset the model to its default state
48 49 50 51 52 |
# File 'lib/storage_room/model.rb', line 48 def reset! super @errors = [] true end |
#save ⇒ Object
Create a new model or update an existing model on the server
60 61 62 |
# File 'lib/storage_room/model.rb', line 60 def save new_record? ? create : update end |
#to_hash(args = {}) ⇒ Object
ActiveSupport caused problems when using as_json, so using to_hash
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/storage_room/model.rb', line 104 def to_hash(args = {}) # :nodoc: args ||= {} if args[:nested] {'url' => self[:@url] || self[:url]} else hash = super hash.merge!('@version' => self['@version']) unless new_record? {self.class.json_name => hash} end end |
#update ⇒ Object
Update an existing model on the server
76 77 78 79 80 81 82 83 84 |
# File 'lib/storage_room/model.rb', line 76 def update return false if new_record? run_callbacks :save do run_callbacks :update do httparty = self.class.put(self[:@url], .merge(:body => to_json)) handle_save_response(httparty) end end end |
#valid? ⇒ Boolean
Is the model valid or were there validation errors on the server?
99 100 101 |
# File 'lib/storage_room/model.rb', line 99 def valid? self.errors.empty? end |