class Chef::EventDispatch::DSL

Attributes

handler[R]

Public Class Methods

new(name) click to toggle source
# File lib/chef/event_dispatch/dsl.rb, line 27
def initialize(name)
  klass = Class.new(Chef::EventDispatch::Base) do
    attr_reader :name
  end
  @handler = klass.new
  @handler.instance_variable_set(:@name, name)

  # Use event.register API to add anonymous handler if Chef.run_context
  # and associated event dispatcher is set, else fallback to
  # Chef::Config[:event_handlers]
  if Chef.run_context && Chef.run_context.events
    Chef::Log.trace("Registering handler '#{name}' using events api")
    Chef.run_context.events.register(handler)
  else
    Chef::Log.trace("Registering handler '#{name}' using global config")
    Chef::Config[:event_handlers] << handler
  end
end

Public Instance Methods

on(event_type, &block) click to toggle source

Adds a new event handler derived from base handler with user defined block against a chef event

@return [Chef::EventDispatch::Base] a base handler object

# File lib/chef/event_dispatch/dsl.rb, line 50
def on(event_type, &block)
  validate!(event_type)
  handler.define_singleton_method(event_type) do |*args|
    instance_exec(*args, &block)
  end
end

Private Instance Methods

validate!(event_type) click to toggle source
# File lib/chef/event_dispatch/dsl.rb, line 59
def validate!(event_type)
  all_event_types = (Chef::EventDispatch::Base.instance_methods - Object.instance_methods)
  raise Chef::Exceptions::InvalidEventType, "Invalid event type: #{event_type}" unless all_event_types.include?(event_type)
end