Class: Brainguy::ManifestEmitter

Inherits:
Emitter
  • Object
show all
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.

->(message) do
  fail UnknownEvent, message, caller.drop_while{|l| l.include?(__FILE__)}
end

Constants inherited from Emitter

Emitter::DEFAULT_NOTIFIER

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • subscription_set (Emitter)

    the set to be wrapped

  • options (Hash) (defaults to: {})

    a hash of options

Options Hash (options):

  • :policy (:call, Symbol)

    the policy for what to do on unknown event names. A callable or a mnemonic symbol. The following mnemonics are supported:

    • :warn: Output a warning
    • :raise_error: Raise an exception


33
34
35
36
37
38
# File 'lib/brainguy/manifest_emitter.rb', line 33

def initialize(subscription_set, options = {})
  super(subscription_set)
  @known_types = []
  policy = options.fetch(:policy) { :warn }
  @policy      = resolve_policy(policy)
end

Instance Attribute Details

#known_typesArray<Symbol> (readonly)

The list of known event names

Returns:

  • (Array<Symbol>)


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.

Parameters:

  • event_name (Symbol)

    the name of the event

  • extra_args (Array)

    any extra arguments that should accompany the event

Returns:

  • the notifier's result value



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.

Overloads:

  • #on(name, &block) ⇒ Object

    Attach a block to be called for a specific event. The block will be called with the event arguments (not the event object).

    Parameters:

    • name (Symbol)
    • block (Proc)

      what to do when the event is emitted

  • #on(handlers) ⇒ Object

    Attach multiple event-specific handlers at once.

    Parameters:

    • handlers (Hash{Symbol => [:call]})

      a map of event names to callable handlers.



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