Module: Protege::HookMixin::ClassMethods
- Defined in:
- lib/protege/extensions/hook_mixin.rb
Overview
Class-level DSL mixed into every hook class via Hook.included.
Instance Method Summary collapse
-
#on(*event_classes, &block) ⇒ void
Declare a handler for one or more event classes.
-
#stateful ⇒ void
Declare that this hook keeps state across a single run.
-
#stateful? ⇒ Boolean
True when this hook was declared
stateful. -
#subscriptions ⇒ Hash{Class => Array<Proc>}
The event subscriptions, keyed by every known event class.
Instance Method Details
#on(*event_classes, &block) ⇒ void
This method returns an undefined value.
Declare a handler for one or more event classes.
Pure declaration: records the block in subscriptions; the event bus is wired separately by
Protege::Subscribers::HookDispatcher. Each argument must be a known Protege::Event subclass.
Listing several registers the same block under each. A single event may carry several handlers;
they run in declaration order. Each block is invoked with the event instance via instance_exec.
53 54 55 56 57 58 59 60 61 |
# File 'lib/protege/extensions/hook_mixin.rb', line 53 def on(*event_classes, &block) event_classes.each do |event_class| unless subscriptions.key?(event_class) raise ArgumentError, "unknown event #{event_class.inspect} (known: #{Protege::Event.all.join(', ')})" end subscriptions[event_class] << block end end |
#stateful ⇒ void
This method returns an undefined value.
Declare that this hook keeps state across a single run.
A stateful hook reuses one instance per correlation_id for the lifetime of a run, so instance
variables set in one handler are visible to later handlers; the instance is evicted when the run
ends. Stateless hooks (the default) get a fresh instance per event.
70 71 72 |
# File 'lib/protege/extensions/hook_mixin.rb', line 70 def stateful @stateful = true end |
#stateful? ⇒ Boolean
Returns true when this hook was declared stateful.
75 76 77 |
# File 'lib/protege/extensions/hook_mixin.rb', line 75 def stateful? @stateful || false end |
#subscriptions ⇒ Hash{Class => Array<Proc>}
The event subscriptions, keyed by every known event class.
Pre-seeded with one entry per Protege::Event.all (each an empty array), so the map is also the
set of valid on targets. Handler blocks accumulate in declaration order.
85 86 87 |
# File 'lib/protege/extensions/hook_mixin.rb', line 85 def subscriptions @subscriptions ||= Protege::Event.all.index_with { [] } end |