Class: Syncano::Resources::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/syncano/resources/base.rb

Overview

Base resource used for inheritance

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, attributes = {}) ⇒ Base

Constructor for base resource

Parameters:

  • client (Syncano::Clients::Base)
  • attributes (Hash) (defaults to: {})

    used in making requests to api (ie. parent id)



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

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/syncano/resources/base.rb', line 6

def attributes
  @attributes
end

#destroyedObject

Returns the value of attribute destroyed.



7
8
9
# File 'lib/syncano/resources/base.rb', line 7

def destroyed
  @destroyed
end

#idObject

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

Parameters:

  • client (Syncano::Clients::Base)
  • scope_parameters (Hash) (defaults to: {})
  • conditions (Hash) (defaults to: {})

Returns:

  • (Array)

    which contains Syncano::Resources::Base objects



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

Parameters:

Returns:



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

Parameters:

  • client (Syncano::Clients::Base)
  • scope_parameters (Hash) (defaults to: {})
  • conditions (Hash) (defaults to: {})

Returns:

  • (Integer)


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

Parameters:

Returns:



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

Parameters:

  • client (Syncano::Clients::Base)
  • key (Integer, String)
  • scope_parameters (Hash) (defaults to: {})
  • conditions (Hash) (defaults to: {})


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

Parameters:

  • attribute_name (Symbol, String)

Returns:

  • (Object)


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

Parameters:

  • attribute_name (Symbol, String)
  • attribute_value (Object)

Returns:

  • (Object)


42
43
44
# File 'lib/syncano/resources/base.rb', line 42

def []=(attribute_name, attribute_value)
  attributes[attribute_name] = attribute_value
end

#batchSyncano::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

Parameters:

  • batch_client (Jimson::BatchClient)

Returns:



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

Parameters:

  • batch_client (Jimson::BatchClient)

Returns:



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

Parameters:

  • batch_client (Jimson::BatchClient)
  • attributes (Hash)

Returns:



121
122
123
# File 'lib/syncano/resources/base.rb', line 121

def batch_update(batch_client, attributes)
  perform_update(batch_client, attributes)
end

#destroySyncano::Resources::Base

Wrapper for api “delete” method Destroys object in Syncano

Returns:



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

Returns:

  • (TrueClass, FalseClass)


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

Returns:

  • (TrueClass, FalseClass)


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

Returns:

  • (TrueClass, FalseClass)


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

#saveSyncano::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

Returns:

  • (TrueClass, FalseClass)


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

Parameters:

  • attributes (Hash)

Returns:



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