Class: Replicate::Emitter
- Inherits:
-
Object
- Object
- Replicate::Emitter
- Defined in:
- lib/replicate/emitter.rb
Overview
Base class for Dumper / Loader classes. Manages a list of callback listeners and dispatches to each when #emit is called.
Instance Method Summary collapse
-
#complete ⇒ Object
Notify all listeners that processing is complete.
-
#emit(type, id, attributes, object) ⇒ Object
Emit an object event to each listener.
-
#initialize ⇒ Emitter
constructor
Yields self to the block and calls #complete when block is finished.
-
#listen(p = nil, &block) ⇒ Object
Register a listener to be called for each loaded object with the type, id, attributes, object structure.
-
#use(klass, *args, &block) ⇒ Object
Sugar for creating a listener with an object instance.
Constructor Details
#initialize ⇒ Emitter
Yields self to the block and calls #complete when block is finished.
6 7 8 9 10 11 12 |
# File 'lib/replicate/emitter.rb', line 6 def initialize @listeners = [] if block_given? yield self complete end end |
Instance Method Details
#complete ⇒ Object
Notify all listeners that processing is complete.
50 51 52 |
# File 'lib/replicate/emitter.rb', line 50 def complete @listeners.each { |p| p.complete if p.respond_to?(:complete) } end |
#emit(type, id, attributes, object) ⇒ Object
Emit an object event to each listener.
Returns the object.
44 45 46 47 |
# File 'lib/replicate/emitter.rb', line 44 def emit(type, id, attributes, object) @listeners.each { |p| p.call(type, id, attributes, object) } object end |
#listen(p = nil, &block) ⇒ Object
Register a listener to be called for each loaded object with the type, id, attributes, object structure. Listeners are executed in the reverse order of which they were registered. Listeners registered later modify the view of listeners registered earlier.
p - An optional Proc object. Must respond to call. block - An optional block.
Returns nothing.
23 24 25 26 |
# File 'lib/replicate/emitter.rb', line 23 def listen(p=nil, &block) @listeners.unshift p if p @listeners.unshift block if block end |
#use(klass, *args, &block) ⇒ Object
Sugar for creating a listener with an object instance. Instances of the class must respond to call(type, id, attrs, object).
klass - The class to create. Must respond to new. args - Arguments to pass to new in addition to self.
Returns the object created.
35 36 37 38 39 |
# File 'lib/replicate/emitter.rb', line 35 def use(klass, *args, &block) instance = klass.new(self, *args, &block) listen instance instance end |