class Syncano::Resources::Base

Base resource used for inheritance

Attributes

attributes[RW]
client[RW]
destroyed[RW]
id[RW]
saved_attributes[RW]

Public Class Methods

all(client, scope_parameters = {}, conditions = {}) click to toggle source

Wrapper for api “get” method Returns all objects from Syncano @param [Syncano::Clients::Base] client @param [Hash] scope_parameters @param [Hash] conditions @return [Array] which contains Syncano::Resources::Base objects

# 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) click to toggle source

Batch version of “create” method @param [Jimson::BatchClient] batch_client @param [Syncano::Clients::Base] client @param [Hash] attributes @return [Syncano::Response]

# 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 = {}) click to toggle source

Returns amount of elements returned from all method @param [Syncano::Clients::Base] client @param [Hash] scope_parameters @param [Hash] conditions @return [Integer]

# 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) click to toggle source

Wrapper for api “new” method Creates object in Syncano @param [Syncano::Clients::Base] client @param [Hash] attributes @return [Syncano::Resources::Base]

# 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 = {}) click to toggle source

Wrapper for api “get_one” method Returns one object from Syncano @param [Syncano::Clients::Base] client @param [Integer, String] key @param [Hash] scope_parameters @param [Hash] conditions

# 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
new(client, attributes = {}) click to toggle source

Constructor for base resource @param [Syncano::Clients::Base] client @param [Hash] attributes used in making requests to api (ie. parent id)

Calls superclass method
# 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

Private Class Methods

api_method(method_name) click to toggle source

Converts Syncano gem method to corresponding Syncano api method @param [String] method_name @return [String]

# File lib/syncano/resources/base.rb, line 292
def self.api_method(method_name)
  mapping = { all: :get, find: :get_one, create: :new, update: :update, destroy: :delete }

  method_name = method_name.to_s.gsub('batch_', '')
  mapping.keys.include?(method_name.to_sym) ? mapping[method_name.to_sym] : method_name
end
api_resource() click to toggle source

Converts resource class name to corresponding Syncano resource name @return [String]

# File lib/syncano/resources/base.rb, line 285
def self.api_resource
  syncano_model_name || to_s.split('::').last.downcase
end
attributes_to_sync(attributes = {}) click to toggle source

Prepares hash with attributes used in synchronization with Syncano @param [Hash] attributes @return [Hash]

# File lib/syncano/resources/base.rb, line 349
def self.attributes_to_sync(attributes = {})
  attributes
end
check_class_method_existance!(method_name) click to toggle source

Checks whether class method is implemented in the resource class

# File lib/syncano/resources/base.rb, line 360
def self.check_class_method_existance!(method_name)
  raise NoMethodError.new("undefined method `#{method_name}' for #{to_s}") unless crud_class_methods.include?(method_name.to_sym)
end
check_if_sync_client!(client) click to toggle source

Checks if sync connection is used

# File lib/syncano/resources/base.rb, line 370
def self.check_if_sync_client!(client)
  raise Syncano::BaseError.new('Operation available only for Sync client') unless client.is_a?(::Syncano::Clients::Sync)
end
make_request(client, batch_client, method_name, attributes = {}, response_key = nil) click to toggle source

Calls request to api through client object @param [Syncano::Clients::Base] client @param [Jimson::BatchClient] batch_client @param [String] method_name @param [Hash] attributes @param [String] response_key @return [Syncano::Response]

# File lib/syncano/resources/base.rb, line 306
def self.make_request(client, batch_client, method_name, attributes = {}, response_key = nil)
  if batch_client.nil?
    client.make_request(api_resource, api_method(method_name), attributes, response_key)
  else
    client.make_batch_request(batch_client, api_resource, api_method(method_name), attributes)
  end
end
map_to_scope_parameters(attributes) click to toggle source

Returns scope parameters from provided hash with attributes @param [Hash] attributes @return [Hash]

# File lib/syncano/resources/base.rb, line 317
def self.map_to_scope_parameters(attributes)
  Hash[scope_parameters.map{ |sym| [sym, attributes[sym]]}]
end
perform_all(client, scope_parameters, conditions) click to toggle source

Executes proper all request @param [Syncano::Clients::Base] client @param [Hash] scope_parameters @param [Hash] conditions @return [Syncano::Response]

# File lib/syncano/resources/base.rb, line 216
def self.perform_all(client, scope_parameters, conditions)
  check_class_method_existance!(:all)
  make_request(client, nil, :all, conditions.merge(scope_parameters))
end
perform_count(client, scope_parameters, conditions) click to toggle source

Executes proper count request @param [Syncano::Clients::Base] client @param [Hash] scope_parameters @param [Hash] conditions @return [Syncano::Response]

# File lib/syncano/resources/base.rb, line 226
def self.perform_count(client, scope_parameters, conditions)
  check_class_method_existance!(:count)
  all(client, scope_parameters, conditions).count
end
perform_create(client, batch_client, attributes) click to toggle source

Executes proper create request @param [Syncano::Clients::Base] client @param [Jimson::BatchClient] batch_client @param [Hash] attributes @return [Syncano::Response]

# File lib/syncano/resources/base.rb, line 248
def self.perform_create(client, batch_client, attributes)
  check_class_method_existance!(:create)
  make_request(client, batch_client, :create, attributes_to_sync(attributes))
end
perform_find(client, key_name, key, scope_parameters, conditions) click to toggle source

Executes proper find request @param [Syncano::Clients::Base] client @param [Symbol, String] key_name @param [Integer, String] key @param [Hash] scope_parameters @param [Hash] conditions @return [Syncano::Response]

# File lib/syncano/resources/base.rb, line 238
def self.perform_find(client, key_name, key, scope_parameters, conditions)
  check_class_method_existance!(:find)
  make_request(client, nil, :find, conditions.merge(scope_parameters.merge(key_name.to_sym => key)))
end
primary_key_name() click to toggle source

Returns name for primary key @return [Hash]

# File lib/syncano/resources/base.rb, line 329
def self.primary_key_name
  "#{api_resource}_#{primary_key}".to_sym
end

Public Instance Methods

[](attribute_name) click to toggle source

Single attribute getter @param [Symbol, String] attribute_name @return [Object]

# File lib/syncano/resources/base.rb, line 34
def [](attribute_name)
  attributes[attribute_name]
end
[]=(attribute_name, attribute_value) click to toggle source

Single attribute setter @param [Symbol, String] attribute_name @param [Object] attribute_value @return [Object]

# File lib/syncano/resources/base.rb, line 42
def []=(attribute_name, attribute_value)
  attributes[attribute_name] = attribute_value
end
attributes=(attributes) click to toggle source

Attributes setter @param [Hash] attributes @return [Hash]

# File lib/syncano/resources/base.rb, line 27
def attributes=(attributes)
  @attributes.merge!(attributes)
end
batch() click to toggle source

Proxy for preparing batch requests ie. resource.batch.update will prepare BatchQueueElement which invokes batch_update method on resource object @return [Syncano::BatchQueueElement]

# File lib/syncano/resources/base.rb, line 50
def batch
  ::Syncano::BatchQueueElement.new(self)
end
batch_destroy(batch_client) click to toggle source

Batch version of “destroy” method @param [Jimson::BatchClient] batch_client @return [Syncano::Response]

# File lib/syncano/resources/base.rb, line 163
def batch_destroy(batch_client)
  perform_destroy(batch_client)
end
batch_save(batch_client) click to toggle source

Batch version of “save” method @param [Jimson::BatchClient] batch_client @return [Syncano::Response]

# File lib/syncano/resources/base.rb, line 147
def batch_save(batch_client)
  perform_save(batch_client)
end
batch_update(batch_client, attributes) click to toggle source

Batch version of “update” method @param [Jimson::BatchClient] batch_client @param [Hash] attributes @return [Syncano::Response]

# File lib/syncano/resources/base.rb, line 121
def batch_update(batch_client, attributes)
  perform_update(batch_client, attributes)
end
destroy() click to toggle source

Wrapper for api “delete” method Destroys object in Syncano @return [Syncano::Resources::Base] marked as destroyed

# File lib/syncano/resources/base.rb, line 154
def destroy
  response = perform_destroy(nil)
  self.destroyed = response.status
  self
end
destroyed?() click to toggle source

Checks whether record is marked as destroyed @return [TrueClass, FalseClass]

# File lib/syncano/resources/base.rb, line 181
def destroyed?
  !!destroyed
end
new_record?() click to toggle source

Checks whether is newly initialized or not @return [TrueClass, FalseClass]

# File lib/syncano/resources/base.rb, line 169
def new_record?
  id.nil?
end
reload!(conditions = {}) click to toggle source

Reloads record from Syncano @return [TrueClass, FalseClass]

# 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() click to toggle source

Invokes create or update methods @return [Syncano::Resources::Base]

# 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?() click to toggle source

Checks whether record is different than stored in Syncano @return [TrueClass, FalseClass]

# File lib/syncano/resources/base.rb, line 175
def saved?
  !new_record? && attributes == saved_attributes
end
update(attributes) click to toggle source

Wrapper for api “update” method Updates object in Syncano @param [Hash] attributes @return [Syncano::Resources::Base]

# 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

Private Instance Methods

attributes_to_sync() click to toggle source

Prepares hash with attributes used in synchronization with Syncano @return [Hash]

# File lib/syncano/resources/base.rb, line 355
def attributes_to_sync
  self.class.attributes_to_sync(attributes)
end
check_if_sync_client!() click to toggle source

Checks if object uses sync connection

# File lib/syncano/resources/base.rb, line 375
def check_if_sync_client!
  self.class.check_if_sync_client!(client)
end
check_instance_method_existance!(method_name) click to toggle source

Checks whether class method is implemented in the resource class

# File lib/syncano/resources/base.rb, line 365
def check_instance_method_existance!(method_name)
  raise NoMethodError.new("undefined method `#{method_name}' for #{to_s}") unless crud_instance_methods.include?(method_name.to_sym)
end
mark_as_saved!() click to toggle source

Marks record as saved, by copying attributes to saved_attributes @return [Integer, String]

# File lib/syncano/resources/base.rb, line 341
def mark_as_saved!
  self.saved_attributes = attributes.dup
  self
end
perform_destroy(batch_client) click to toggle source

Executes proper destroy request @param [Jimson::BatchClient] batch_client @return [Syncano::Response]

# File lib/syncano/resources/base.rb, line 278
def perform_destroy(batch_client)
  check_instance_method_existance!(:destroy)
  self.class.make_request(client, batch_client, :destroy, scope_parameters.merge({ self.class.primary_key_name => primary_key }))
end
perform_save(batch_client) click to toggle source

Executes proper save request @param [Jimson::BatchClient] batch_client @return [Syncano::Response]

# File lib/syncano/resources/base.rb, line 265
def perform_save(batch_client)
  check_instance_method_existance!(:save)

  if new_record?
    self.class.perform_create(client, batch_client, attributes)
  else
    perform_update(batch_client, attributes)
  end
end
perform_update(batch_client, attributes) click to toggle source

Executes proper update request @param [Jimson::BatchClient] batch_client @param [Hash] attributes @return [Syncano::Response]

# File lib/syncano/resources/base.rb, line 257
def perform_update(batch_client, attributes)
  check_instance_method_existance!(:update)
  self.class.make_request(client, batch_client, :update, scope_parameters.merge(self.class.attributes_to_sync(attributes).merge(self.class.primary_key_name => primary_key)))
end
primary_key() click to toggle source

Returns value of primary key @return [Integer, String]

# File lib/syncano/resources/base.rb, line 335
def primary_key
  self.class.primary_key == :id ? id : @saved_attributes[self.class.primary_key]
end
scope_parameters() click to toggle source

Returns scope parameters from object's attributes @return [Hash]

# File lib/syncano/resources/base.rb, line 323
def scope_parameters
  self.class.map_to_scope_parameters(attributes)
end