Class: Chef::Mixin::WhyRun::ConvergeActions

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/mixin/why_run.rb

Overview

ConvergeActions

ConvergeActions implements the logic for why run. A ConvergeActions object wraps a collection of actions, which consist of a descriptive string and a block/Proc. Actions are executed by calling #converge! When why_run mode is enabled, each action’s description will be printed, but the block will not be called. Conversely, in normal mode, the block is called, but the message is not printed.

In general, this class should be accessed through the API provided by Chef::Provider.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, run_context, action) ⇒ ConvergeActions

Returns a new instance of ConvergeActions.



37
38
39
40
# File 'lib/chef/mixin/why_run.rb', line 37

def initialize(resource, run_context, action)
  @resource, @run_context = resource, run_context
  @actions = []
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



35
36
37
# File 'lib/chef/mixin/why_run.rb', line 35

def actions
  @actions
end

Instance Method Details

#add_action(descriptions, &block) ⇒ Object

Adds an action to the list. descriptions can either be an Array of Strings, or a single String describing the action; block is a block/proc that implements the action.



49
50
51
# File 'lib/chef/mixin/why_run.rb', line 49

def add_action(descriptions, &block)
  @actions << [descriptions, block]
end

#converge!Object

Iterate over the actions, and either print the action’s message, or run its code block, depending on whether why_run mode is active.



60
61
62
63
64
65
66
67
# File 'lib/chef/mixin/why_run.rb', line 60

def converge!
  @actions.each do |descriptions, block|
    if !Chef::Config[:why_run]
      block.call
    end
    events.resource_update_applied(@resource, @action, descriptions)
  end
end

#empty?Boolean

True if there are no actions to execute.

Returns:

  • (Boolean)


54
55
56
# File 'lib/chef/mixin/why_run.rb', line 54

def empty?
  @actions.empty?
end

#eventsObject



42
43
44
# File 'lib/chef/mixin/why_run.rb', line 42

def events
  @run_context.events
end