Class: Chef::EventDispatch::DSL
- Inherits:
-
Object
- Object
- Chef::EventDispatch::DSL
- Defined in:
- lib/chef/event_dispatch/dsl.rb
Instance Attribute Summary collapse
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
Instance Method Summary collapse
-
#initialize(name) ⇒ DSL
constructor
A new instance of DSL.
-
#on(event_type, &block) ⇒ Chef::EventDispatch::Base
Adds a new event handler derived from base handler with user defined block against a chef event.
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
#handler ⇒ Object (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
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 |