Class: DiasporaFederation::Callbacks
- Inherits:
-
Object
- Object
- DiasporaFederation::Callbacks
- Defined in:
- lib/diaspora_federation/callbacks.rb
Overview
Callbacks are used to communicate with the application. They are called to fetch data and after data is received.
Instance Method Summary collapse
-
#definition_complete? ⇒ Boolean
Checks if all callbacks are defined.
-
#initialize(events) ⇒ Callbacks
constructor
Initializes a new Callbacks object with the event-keys that need to be defined.
-
#missing_handlers ⇒ Hash
Returns all undefined callbacks.
-
#on(event, &callback) ⇒ Object
Defines a callback.
-
#trigger(event, *args) ⇒ Object
Triggers a callback.
Constructor Details
#initialize(events) ⇒ Callbacks
Initializes a new Callbacks object with the event-keys that need to be defined
16 17 18 19 |
# File 'lib/diaspora_federation/callbacks.rb', line 16 def initialize(events) @events = events @handlers = {} end |
Instance Method Details
#definition_complete? ⇒ Boolean
Checks if all callbacks are defined
54 55 56 |
# File 'lib/diaspora_federation/callbacks.rb', line 54 def definition_complete? missing_handlers.empty? end |
#missing_handlers ⇒ Hash
Returns all undefined callbacks
60 61 62 |
# File 'lib/diaspora_federation/callbacks.rb', line 60 def missing_handlers @events - @handlers.keys end |
#on(event, &callback) ⇒ Object
Defines a callback
31 32 33 34 35 36 |
# File 'lib/diaspora_federation/callbacks.rb', line 31 def on(event, &callback) raise ArgumentError, "Undefined event #{event}" unless @events.include? event raise ArgumentError, "Already defined event #{event}" if @handlers.has_key? event @handlers[event] = callback end |
#trigger(event, *args) ⇒ Object
Triggers a callback
46 47 48 49 50 |
# File 'lib/diaspora_federation/callbacks.rb', line 46 def trigger(event, *args) raise ArgumentError, "Undefined event #{event}" unless @events.include? event @handlers[event].call(*args) end |