Class: Brainguy::ManifestEmitter
- Defined in:
- lib/brainguy/manifest_emitter.rb
Overview
A Emitter wrapper which "locks down" a subscription set to a known list of event names. Useful for preventing typos in event names.
Constant Summary collapse
- WARN_POLICY =
A policy which outputs a warning on unrecognized event names
Kernel.method(:warn)
- RAISE_ERROR_POLICY =
A policy which raises an exception on unrecognized evend names.
->() do fail UnknownEvent, , caller.drop_while{|l| l.include?(__FILE__)} end
Constants inherited from Emitter
Instance Attribute Summary collapse
-
#known_types ⇒ Array<Symbol>
readonly
The list of known event names.
Instance Method Summary collapse
-
#emit(event_name) ⇒ Object
Emit an event to be distributed to all interested listeners.
-
#initialize(subscription_set, options = {}) ⇒ ManifestEmitter
constructor
A new instance of ManifestEmitter.
-
#on(event_name, &block) ⇒ Object
Attach blocks of code to handle specific named events.
- #unknown_event_policy=(new_policy) ⇒ Object
Methods inherited from Emitter
#attach, #detach, new_from_existing, #subscriptions, #with_subscription_scope
Constructor Details
#initialize(subscription_set, options = {}) ⇒ ManifestEmitter
Returns a new instance of ManifestEmitter.
33 34 35 36 37 38 |
# File 'lib/brainguy/manifest_emitter.rb', line 33 def initialize(subscription_set, = {}) super(subscription_set) @known_types = [] policy = .fetch(:policy) { :warn } @policy = resolve_policy(policy) end |
Instance Attribute Details
#known_types ⇒ Array<Symbol> (readonly)
The list of known event names
23 24 25 |
# File 'lib/brainguy/manifest_emitter.rb', line 23 def known_types @known_types end |
Instance Method Details
#emit(event_name) ⇒ Object
Emit an event to be distributed to all interested listeners.
52 53 54 55 |
# File 'lib/brainguy/manifest_emitter.rb', line 52 def emit(event_name, *) check_event_name(event_name, __callee__) super end |
#on(name, &block) ⇒ Object #on(handlers) ⇒ Object
Attach blocks of code to handle specific named events.
46 47 48 49 |
# File 'lib/brainguy/manifest_emitter.rb', line 46 def on(event_name, &block) check_event_name(event_name, __callee__) super end |
#unknown_event_policy=(new_policy) ⇒ Object
41 42 43 |
# File 'lib/brainguy/manifest_emitter.rb', line 41 def unknown_event_policy=(new_policy) @policy = resolve_policy(new_policy) end |