Class: Syncano::Resources::Base
- Inherits:
-
Object
- Object
- Syncano::Resources::Base
- Defined in:
- lib/syncano/resources/base.rb
Overview
Base resource used for inheritance
Direct Known Subclasses
Admin, ApiKey, Collection, DataObject, Folder, Notifications::Base, Project, Role, Subscription, User
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#destroyed ⇒ Object
readonly
Returns the value of attribute destroyed.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.all(client, scope_parameters = {}, conditions = {}) ⇒ Array
Wrapper for api “get” method Returns all objects from Syncano.
-
.batch_create(batch_client, client, attributes) ⇒ Syncano::Response
Batch version of “create” method.
-
.count(client, scope_parameters = {}, conditions = {}) ⇒ Integer
Returns amount of elements returned from all method.
-
.create(client, attributes) ⇒ Syncano::Resources::Base
Wrapper for api “new” method Creates object in Syncano.
-
.find(client, key, scope_parameters = {}, conditions = {}) ⇒ Object
Wrapper for api “get_one” method Returns one object from Syncano.
Instance Method Summary collapse
-
#[](attribute_name) ⇒ Object
Single attribute getter.
-
#[]=(attribute_name, attribute_value) ⇒ Object
Single attribute setter.
-
#batch ⇒ Syncano::BatchQueueElement
Proxy for preparing batch requests ie.
-
#batch_destroy(batch_client) ⇒ Syncano::Response
Batch version of “destroy” method.
-
#batch_save(batch_client) ⇒ Syncano::Response
Batch version of “save” method.
-
#batch_update(batch_client, attributes) ⇒ Syncano::Response
Batch version of “update” method.
-
#destroy ⇒ Syncano::Resources::Base
Wrapper for api “delete” method Destroys object in Syncano.
-
#destroyed? ⇒ TrueClass, FalseClass
Checks whether record is marked as destroyed.
-
#initialize(client, attributes = {}) ⇒ Base
constructor
Constructor for base resource.
-
#new_record? ⇒ TrueClass, FalseClass
Checks whether is newly initialized or not.
-
#reload!(conditions = {}) ⇒ TrueClass, FalseClass
Reloads record from Syncano.
-
#save ⇒ Syncano::Resources::Base
Invokes create or update methods.
-
#saved? ⇒ TrueClass, FalseClass
Checks whether record is different than stored in Syncano.
-
#update(attributes) ⇒ Syncano::Resources::Base
Wrapper for api “update” method Updates object in Syncano.
Constructor Details
#initialize(client, attributes = {}) ⇒ Base
Constructor for base resource
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/syncano/resources/base.rb', line 12 def initialize(client, attributes = {}) super() @attributes = ActiveSupport::HashWithIndifferentAccess.new(attributes) @saved_attributes = ActiveSupport::HashWithIndifferentAccess.new self.id = @attributes.delete(:id) self.client = client mark_as_saved! if id.present? end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/syncano/resources/base.rb', line 6 def attributes @attributes end |
#destroyed ⇒ Object
Returns the value of attribute destroyed.
7 8 9 |
# File 'lib/syncano/resources/base.rb', line 7 def destroyed @destroyed end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/syncano/resources/base.rb', line 7 def id @id end |
Class Method Details
.all(client, scope_parameters = {}, conditions = {}) ⇒ Array
Wrapper for api “get” method Returns all objects from Syncano
60 61 62 63 64 65 |
# File 'lib/syncano/resources/base.rb', line 60 def self.all(client, scope_parameters = {}, conditions = {}) response = perform_all(client, scope_parameters, conditions) response.data.to_a.collect do |attributes| new(client, attributes.merge(scope_parameters)) end end |
.batch_create(batch_client, client, attributes) ⇒ Syncano::Response
Batch version of “create” method
102 103 104 |
# File 'lib/syncano/resources/base.rb', line 102 def self.batch_create(batch_client, client, attributes) perform_create(client, batch_client, attributes) end |
.count(client, scope_parameters = {}, conditions = {}) ⇒ Integer
Returns amount of elements returned from all method
72 73 74 |
# File 'lib/syncano/resources/base.rb', line 72 def self.count(client, scope_parameters = {}, conditions = {}) perform_count(client, scope_parameters, conditions) end |
.create(client, attributes) ⇒ Syncano::Resources::Base
Wrapper for api “new” method Creates object in Syncano
92 93 94 95 |
# File 'lib/syncano/resources/base.rb', line 92 def self.create(client, attributes) response = perform_create(client, nil, attributes) new(client, map_to_scope_parameters(attributes).merge(response.data)) end |
.find(client, key, scope_parameters = {}, conditions = {}) ⇒ Object
Wrapper for api “get_one” method Returns one object from Syncano
82 83 84 85 |
# File 'lib/syncano/resources/base.rb', line 82 def self.find(client, key, scope_parameters = {}, conditions = {}) response = perform_find(client, primary_key_name, key, scope_parameters, conditions) new(client, scope_parameters.merge(response.data)) end |
Instance Method Details
#[](attribute_name) ⇒ Object
Single attribute getter
34 35 36 |
# File 'lib/syncano/resources/base.rb', line 34 def [](attribute_name) attributes[attribute_name] end |
#[]=(attribute_name, attribute_value) ⇒ Object
Single attribute setter
42 43 44 |
# File 'lib/syncano/resources/base.rb', line 42 def []=(attribute_name, attribute_value) attributes[attribute_name] = attribute_value end |
#batch ⇒ Syncano::BatchQueueElement
Proxy for preparing batch requests ie. resource.batch.update will prepare BatchQueueElement which invokes batch_update method on resource object
50 51 52 |
# File 'lib/syncano/resources/base.rb', line 50 def batch ::Syncano::BatchQueueElement.new(self) end |
#batch_destroy(batch_client) ⇒ Syncano::Response
Batch version of “destroy” method
163 164 165 |
# File 'lib/syncano/resources/base.rb', line 163 def batch_destroy(batch_client) perform_destroy(batch_client) end |
#batch_save(batch_client) ⇒ Syncano::Response
Batch version of “save” method
147 148 149 |
# File 'lib/syncano/resources/base.rb', line 147 def batch_save(batch_client) perform_save(batch_client) end |
#batch_update(batch_client, attributes) ⇒ Syncano::Response
Batch version of “update” method
121 122 123 |
# File 'lib/syncano/resources/base.rb', line 121 def batch_update(batch_client, attributes) perform_update(batch_client, attributes) end |
#destroy ⇒ Syncano::Resources::Base
Wrapper for api “delete” method Destroys object in Syncano
154 155 156 157 158 |
# File 'lib/syncano/resources/base.rb', line 154 def destroy response = perform_destroy(nil) self.destroyed = response.status self end |
#destroyed? ⇒ TrueClass, FalseClass
Checks whether record is marked as destroyed
181 182 183 |
# File 'lib/syncano/resources/base.rb', line 181 def destroyed? !!destroyed end |
#new_record? ⇒ TrueClass, FalseClass
Checks whether is newly initialized or not
169 170 171 |
# File 'lib/syncano/resources/base.rb', line 169 def new_record? id.nil? end |
#reload!(conditions = {}) ⇒ TrueClass, FalseClass
Reloads record from Syncano
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/syncano/resources/base.rb', line 187 def reload!(conditions = {}) unless new_record? reloaded_object = self.class.find(client, primary_key, scope_parameters, conditions) self.attributes.clear self.attributes = reloaded_object.attributes mark_as_saved! end self end |
#save ⇒ Syncano::Resources::Base
Invokes create or update methods
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/syncano/resources/base.rb', line 127 def save response = perform_save(nil) response_data = ActiveSupport::HashWithIndifferentAccess.new(response.data) if new_record? created_object = self.class.new(client, self.class.map_to_scope_parameters(attributes).merge(response_data)) self.id = created_object.id self.attributes.merge!(created_object.attributes) else self[:updated_at] = response_data[:updated_at] end mark_as_saved! self end |
#saved? ⇒ TrueClass, FalseClass
Checks whether record is different than stored in Syncano
175 176 177 |
# File 'lib/syncano/resources/base.rb', line 175 def saved? !new_record? && attributes == saved_attributes end |
#update(attributes) ⇒ Syncano::Resources::Base
Wrapper for api “update” method Updates object in Syncano
110 111 112 113 114 115 |
# File 'lib/syncano/resources/base.rb', line 110 def update(attributes) response = perform_update(nil, attributes) response.data.delete('id') self.attributes = scope_parameters.merge(response.data) mark_as_saved! end |