Class: Streamio::Model
- Inherits:
-
Object
- Object
- Streamio::Model
- Defined in:
- lib/streamio/model.rb
Constant Summary collapse
- CASTED_ATTRIBUTES =
%w(tags created_at updated_at)
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
A Hash resulting from parsing the JSON returned from Streamios API.
-
#errors ⇒ Object
readonly
A Hash containing validation errors after a failed
save
.
Class Method Summary collapse
- .accessable_attributes(attributes = nil) ⇒ Object
-
.all(parameters = {}) ⇒ Array
Querys for a list of models.
-
.count(parameters = {}) ⇒ Integer
Returns a count of number of models on your account filtered on the parameters your specify.
- .creatable_attributes(attributes = nil) ⇒ Object
-
.create(attributes = {}) ⇒ Model
Initializes a new model instance with the given attributes, saves it and returns it.
-
.destroy(id) ⇒ Boolean
Deletes model with the given id.
-
.find(id) ⇒ Model
Gets a model by its id.
- .readable_attributes(attributes = nil) ⇒ Object
- .resource ⇒ Object
- .resource_name(name) ⇒ Object
Instance Method Summary collapse
-
#created_at ⇒ Time
When the record was created.
-
#destroy ⇒ Boolean
Deletes the record and freezes this instance to reflect that no changes should be made (since they can’t be persisted).
-
#destroyed? ⇒ Boolean
True if you destroyed this record.
-
#initialize(attributes = {}) ⇒ Model
constructor
A new instance of Model.
-
#persisted? ⇒ Boolean
True if the record is persisted.
-
#reload ⇒ Model
Update the model instance with the current state on the remote.
-
#save ⇒ Boolean
Saves the model.
-
#tags ⇒ Array
Array of tags applied to the record.
-
#updated_at ⇒ Time
When the record was last updated.
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
136 137 138 139 140 141 142 |
# File 'lib/streamio/model.rb', line 136 def initialize(attributes = {}) @errors = {} @attributes = attributes.inject(Hash.new) do |, (key, value)| [key.to_s] = value end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
A Hash resulting from parsing the JSON returned from Streamios API.
130 131 132 |
# File 'lib/streamio/model.rb', line 130 def attributes @attributes end |
#errors ⇒ Object (readonly)
A Hash containing validation errors after a failed save
.
133 134 135 |
# File 'lib/streamio/model.rb', line 133 def errors @errors end |
Class Method Details
.accessable_attributes(attributes = nil) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/streamio/model.rb', line 77 def accessable_attributes(attributes = nil) return @accessable_attributes ||= [] if attributes.nil? @accessable_attributes = attributes create_getters(attributes) create_setters(attributes) end |
.all(parameters = {}) ⇒ Array
Querys for a list of models.
for the query. Refer to Streamio API reference for a list of valid parameters for each of the different models availible.
20 21 22 23 24 |
# File 'lib/streamio/model.rb', line 20 def all(parameters = {}) sanitize_parameters(parameters) response = resource.get(nil, parameters) parse_response(response) end |
.count(parameters = {}) ⇒ Integer
Returns a count of number of models on your account filtered on the parameters your specify.
for the count. Refer to Streamio API reference for a list of valid parameters for each of the different models availible.
55 56 57 58 59 |
# File 'lib/streamio/model.rb', line 55 def count(parameters = {}) sanitize_parameters(parameters) response = resource.get("count", parameters) MultiJson.decode(response.body)["count"] end |
.creatable_attributes(attributes = nil) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/streamio/model.rb', line 69 def creatable_attributes(attributes = nil) return @creatable_attributes ||= [] if attributes.nil? @creatable_attributes = attributes create_getters(attributes) create_setters(attributes) end |
.create(attributes = {}) ⇒ Model
Initializes a new model instance with the given attributes, saves it and returns it.
42 43 44 45 46 |
# File 'lib/streamio/model.rb', line 42 def create(attributes = {}) model = new(attributes) model.save model end |
.destroy(id) ⇒ Boolean
Deletes model with the given id. Raises exception if failing to do so.
31 32 33 34 |
# File 'lib/streamio/model.rb', line 31 def destroy(id) resource.delete(id) true end |
.find(id) ⇒ Model
Gets a model by its id.
9 10 11 |
# File 'lib/streamio/model.rb', line 9 def find(id) parse_response(resource.get(id)) end |
.readable_attributes(attributes = nil) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/streamio/model.rb', line 85 def readable_attributes(attributes = nil) return @readable_attributes ||= [] if attributes.nil? @readable_attributes = attributes create_getters(attributes) end |
.resource ⇒ Object
65 66 67 |
# File 'lib/streamio/model.rb', line 65 def resource Resource.new(@resource_name) end |
.resource_name(name) ⇒ Object
61 62 63 |
# File 'lib/streamio/model.rb', line 61 def resource_name(name) @resource_name = name end |
Instance Method Details
#created_at ⇒ Time
Returns When the record was created.
197 198 199 200 |
# File 'lib/streamio/model.rb', line 197 def created_at return nil unless @attributes["created_at"] Time.parse(@attributes["created_at"]) end |
#destroy ⇒ Boolean
Deletes the record and freezes this instance to reflect that no changes should be made (since they can’t be persisted).
165 166 167 168 169 |
# File 'lib/streamio/model.rb', line 165 def destroy self.class.resource.delete(id) @attributes.freeze true end |
#destroyed? ⇒ Boolean
Returns True if you destroyed this record.
186 187 188 |
# File 'lib/streamio/model.rb', line 186 def destroyed? @attributes.frozen? end |
#persisted? ⇒ Boolean
Returns True if the record is persisted.
181 182 183 |
# File 'lib/streamio/model.rb', line 181 def persisted? !destroyed? && !id.nil? end |
#reload ⇒ Model
Update the model instance with the current state on the remote.
174 175 176 177 178 |
# File 'lib/streamio/model.rb', line 174 def reload remote = self.class.find(id) @attributes = remote.attributes self end |
#save ⇒ Boolean
Saves the model.
If the model is new a record gets created, otherwise the existing record gets updated.
If save
fails it might be due to validation errors so you might want to check the models errors
.
153 154 155 156 157 158 159 |
# File 'lib/streamio/model.rb', line 153 def save if persisted? update else persist end end |
#tags ⇒ Array
Returns Array of tags applied to the record.
191 192 193 194 |
# File 'lib/streamio/model.rb', line 191 def @attributes["tags"] = [] if @attributes["tags"].nil? @attributes["tags"] end |
#updated_at ⇒ Time
Returns When the record was last updated.
203 204 205 206 |
# File 'lib/streamio/model.rb', line 203 def updated_at return nil unless @attributes["updated_at"] Time.parse(@attributes["updated_at"]) end |