class Chef::EventDispatch::Dispatcher

EventDispatch::Dispatcher

The Dispatcher handles receiving event data from the sources (Chef::Client, Resources and Providers, etc.) and publishing the data to the registered subscribers.

Attributes

subscribers[R]

Public Class Methods

new(*subscribers) click to toggle source
# File lib/chef/event_dispatch/dispatcher.rb, line 14
def initialize(*subscribers)
  @subscribers = subscribers
end

Public Instance Methods

call_subscribers(method_name, *args) click to toggle source

All messages are unconditionally forwarded to all subscribers, so just define the forwarding in one go:

# File lib/chef/event_dispatch/dispatcher.rb, line 33
def call_subscribers(method_name, *args)
  @subscribers.each do |s|
    # Skip new/unsupported event names.
    next if !s.respond_to?(method_name)
    mth = s.method(method_name)
    # Trim arguments to match what the subscriber expects to allow
    # adding new arguments without breaking compat.
    if mth.arity < args.size && mth.arity >= 0
      mth.call(*args.take(mth.arity))
    else
      mth.call(*args)
    end
  end
end
deprecation(message, location = caller(2..2)[0]) click to toggle source

Special case deprecation, since it needs to know its caller

# File lib/chef/event_dispatch/dispatcher.rb, line 57
def deprecation(message, location = caller(2..2)[0])
  call_subscribers(:deprecation, message, location)
end
formatter?() click to toggle source

Check to see if we are dispatching to a formatter

# File lib/chef/event_dispatch/dispatcher.rb, line 24
def formatter?
  @subscribers.any? { |s| s.respond_to?(:is_formatter?) && s.is_formatter? }
end
register(subscriber) click to toggle source

Add a new subscriber to the list of registered subscribers

# File lib/chef/event_dispatch/dispatcher.rb, line 19
def register(subscriber)
  @subscribers << subscriber
end