Class: ActiveSpy::Rails::Listener
- Inherits:
-
Object
- Object
- ActiveSpy::Rails::Listener
- Includes:
- ActiveSupport::Inflector
- Defined in:
- lib/active_spy/rails/listener.rb
Overview
Base class used to process the events received.
Constant Summary collapse
- MODEL_HANDLER =
Constant to hold the model translations. The key is the incoming
ref_type
and the value is the matching model class. {}
Class Method Summary collapse
-
.external_class(klass) ⇒ Object
Set the external class of the model that we are listening to.
-
.external_classes(*classes) ⇒ Object
Set the external classes which we are listening to.
-
.hook_classes ⇒ Object
Get the classes that we are listegnig to.
-
.inherited(child) ⇒ Object
Store the event handler hook in the HookList for later registration of them within the event runner.
-
.to_hook ⇒ Object
Convert the listener class into one or more hook hashes.
Instance Method Summary collapse
-
#create(object_type, payload, _actor, _realm) ⇒ Object
Logic to handle object’s creation.
-
#destroy(klass, payload, _actor, _realm) ⇒ Object
Destroy a record from our database.
-
#get_object_class(object_type) ⇒ Object
Gets the object class.
-
#handle(params) ⇒ Object
Handle a request with
params
and sync the database according to them. -
#sync_database(callback, object_type, payload, actor, realm) ⇒ Object
Calls the proper method to sync the database.
-
#update(object_type, payload, _actor, _realm) ⇒ Object
Logic to handle object’s update.
Class Method Details
.external_class(klass) ⇒ Object
Set the external class of the model that we are listening to.
26 27 28 |
# File 'lib/active_spy/rails/listener.rb', line 26 def self.external_class(klass) @external_classes = [klass] end |
.external_classes(*classes) ⇒ Object
Set the external classes which we are listening to.
32 33 34 |
# File 'lib/active_spy/rails/listener.rb', line 32 def self.external_classes(*classes) @external_classes = classes end |
.hook_classes ⇒ Object
Get the classes that we are listegnig to.
48 49 50 51 |
# 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) ⇒ Object
Store the event handler hook in the HookList for later registration of them within the event runner.
20 21 22 |
# File 'lib/active_spy/rails/listener.rb', line 20 def self.inherited(child) ActiveSpy::Rails::HookList << child end |
.to_hook ⇒ Object
Convert the listener class into one or more hook hashes
38 39 40 41 42 43 44 |
# 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 |
Instance Method Details
#create(object_type, payload, _actor, _realm) ⇒ Object
Logic to handle object’s creation. You can override this, as you wish, to suit your own needs
76 77 78 79 80 81 |
# 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) ⇒ Object
Destroy a record from our database. You can override this, as you wish, to suit your own needs
97 98 99 100 101 102 103 |
# 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) ⇒ Object
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
.
110 111 112 113 114 |
# 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) ⇒ Object
Handle a request with params
and sync the database according to them.
56 57 58 59 60 61 62 63 |
# 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) ⇒ Object
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
.
69 70 71 |
# 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) ⇒ Object
Logic to handle object’s update. You can override this, as you wish, to suit your own needs
86 87 88 89 90 91 92 |
# 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 |