module Apiway::Resource::InstanceMethods

Attributes

client[R]
params[R]

Public Class Methods

new( id, client ) click to toggle source
# File lib/apiway/resource.rb, line 40
def initialize( id, client )
  @id     = id
  @client = client
end

Public Instance Methods

set_params( params = {} ) click to toggle source
# File lib/apiway/resource.rb, line 45
def set_params( params = {} )
  @params        = params
  @current_error = nil
  self
end
sync() click to toggle source
# File lib/apiway/resource.rb, line 55
def sync
  begin
    instance_eval &self.class.access
  rescue ResourceError => e
    sync_error e.params
  else
    sync_data instance_eval &self.class.data
  end
end
sync_changes( changed_models ) click to toggle source
# File lib/apiway/resource.rb, line 51
def sync_changes( changed_models )
  sync if self.class.depend_on.any? { |dependency| changed_models.include? dependency }
end

Protected Instance Methods

error( params ) click to toggle source
# File lib/apiway/resource.rb, line 70
def error( params )
  raise ResourceError, params
end

Private Instance Methods

sync_data( data ) click to toggle source
# File lib/apiway/resource.rb, line 93
def sync_data( data )
  @current_error = nil
  new_data_json = JSON.generate data, quirks_mode: true
  if !@current_data || @current_data != new_data_json
    patch         = Diff.new( @current_data, new_data_json ).patch
    patch_json    = JSON.generate patch, quirks_mode: true
    params_sync   = @current_data && patch_json.size < new_data_json.size ? sync_params( patch: patch ) : sync_params( full: data )
    @current_data = new_data_json
    @client.trigger RESOURCE::SYNC, params_sync
  end
end
sync_error( error ) click to toggle source
# File lib/apiway/resource.rb, line 85
def sync_error( error )
  new_error_json = JSON.generate error, quirks_mode: true
  if !@current_error || @current_error != new_error_json
    @current_error = new_error_json
    @client.trigger RESOURCE::SYNC, sync_params( error: error )
  end
end
sync_params( error: nil, full: nil, patch: nil ) click to toggle source
# File lib/apiway/resource.rb, line 77
def sync_params( error: nil, full: nil, patch: nil )
  params           = { id: @id }
  params[ :error ] = error if error
  params[ :full ]  = full  if full
  params[ :patch ] = patch if patch
  params
end