Class: Chef::EventDispatch::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/event_dispatch/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ DSL

Returns a new instance of DSL.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



25
26
27
# File 'lib/chef/event_dispatch/dsl.rb', line 25

def handler
  @handler
end

Instance Method Details

#on(event_type, &block) ⇒ Chef::EventDispatch::Base

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

Returns:



50
51
52
53
54
55
# 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