class ActiveSpy::Rails::Listener

Base class used to process the events received.

Constants

MODEL_HANDLER

Constant to hold the model translations. The key is the incoming ref_type and the value is the matching model class.

Public Class Methods

external_class(klass) click to toggle source

Set the external class of the model that we are listening to.

# File lib/active_spy/rails/listener.rb, line 26
def self.external_class(klass)
  @external_classes = [klass]
end
external_classes(*classes) click to toggle source

Set the external classes which we are listening to.

# File lib/active_spy/rails/listener.rb, line 32
def self.external_classes(*classes)
  @external_classes = classes
end
hook_classes() click to toggle source

Get the classes that we are listegnig to.

# File lib/active_spy/rails/listener.rb, line 48
def self.hook_classes
  return @external_classes if defined? @external_classes
  [name.split('Listener')[0]]
end
inherited(child) click to toggle source

Store the event handler hook in the {ActiveSpy::Rails::HookList} for later registration of them within the event runner.

# File lib/active_spy/rails/listener.rb, line 20
def self.inherited(child)
  ActiveSpy::Rails::HookList << child
end
to_hook() click to toggle source

Convert the listener class into one or more hook hashes

# File lib/active_spy/rails/listener.rb, line 38
def self.to_hook
  hooks = []
  hook_classes.each do |hook_class|
    hooks << { 'class' => hook_class, 'post_class' => name.split('Listener')[0] }
  end
  hooks
end

Public Instance Methods

create(object_type, payload, _actor, _realm) click to toggle source

Logic to handle object’s creation. You can override this, as you wish, to suit your own needs

# File lib/active_spy/rails/listener.rb, line 76
def create(object_type, payload, _actor, _realm)
  klass = get_object_class(object_type)
  object = klass.new
  object.update_attributes(payload)
  object
end
destroy(klass, payload, _actor, _realm) click to toggle source

Destroy a record from our database. You can override this, as you wish, to suit your own needs

# File lib/active_spy/rails/listener.rb, line 97
def destroy(klass, payload, _actor, _realm)
  klass = get_object_class(klass)
  guid = payload.delete('guid')
  object = klass.find_by(guid: guid)
  object.destroy!
  object
end
get_object_class(object_type) click to toggle source

Gets the object class. First, it’ll look the {MODEL_HANDLER} hash and see if there is any translation for a given object_type. If it does not have a translation, this method will try to constantize the object_type.

# File lib/active_spy/rails/listener.rb, line 110
def get_object_class(object_type)
  translated_object_type = MODEL_HANDLER[object_type]
  return constantize(translated_object_type) if translated_object_type
  constantize(object_type)
end
handle(params) click to toggle source

Handle a request with params and sync the database according to them.

# File lib/active_spy/rails/listener.rb, line 56
def handle(params)
  object_type = params.delete('type')
  callback = params.delete('action')
  payload_content = params.delete('payload')[object_type.downcase]
  actor = params.delete('actor')
  realm = params.delete('realm')
  sync_database(callback, object_type, payload_content, actor, realm)
end
sync_database(callback, object_type, payload, actor, realm) click to toggle source

Calls the proper method to sync the database. It will manipulate objects of the class object_type, with the attributes sent in the payload, triggered by the callback callback.

# File lib/active_spy/rails/listener.rb, line 69
def sync_database(callback, object_type, payload, actor, realm)
  send(callback, object_type, payload, actor, realm)
end
update(object_type, payload, _actor, _realm) click to toggle source

Logic to handle object’s update. You can override this, as you wish, to suit your own needs

# File lib/active_spy/rails/listener.rb, line 86
def update(object_type, payload, _actor, _realm)
  klass = get_object_class(object_type)
  guid = payload['guid']
  object = klass.find_by(guid: guid)
  object.update_attributes(payload)
  object
end