diff options
-rw-r--r-- | lib/chef/resource/conditional.rb | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/lib/chef/resource/conditional.rb b/lib/chef/resource/conditional.rb index 30abfdec81..d188e2ad0b 100644 --- a/lib/chef/resource/conditional.rb +++ b/lib/chef/resource/conditional.rb @@ -45,27 +45,37 @@ class Chef def initialize(positivity, parent_resource, command=nil, command_opts={}, &block) @positivity = positivity - case command + @command, @command_opts = command, command_opts + @block = block + @block_given = block_given? + @parent_resource = parent_resource + + raise ArgumentError, "only_if/not_if requires either a command or a block" unless command || block_given? + end + + def configure + case @command when String - @guard_interpreter = new_guard_interpreter(parent_resource, command, command_opts, &block) - @command, @command_opts = command, command_opts + @guard_interpreter = new_guard_interpreter(@parent_resource, @command, @command_opts, &@block) @block = nil when nil - raise ArgumentError, "only_if/not_if requires either a command or a block" unless block_given? - if parent_resource.guard_interpreter != :default - msg = "#{parent_resource.name} was given a guard_interpreter of #{parent_resource.guard_interpreter}, " + if @parent_resource.guard_interpreter != :default + msg = "#{@parent_resource.name} was given a guard_interpreter of #{@parent_resource.guard_interpreter}, " msg << "but not given a command as a string. guard_interpreter does not support blocks (because they just contain ruby)." raise ArgumentError, msg end + @guard_interpreter = nil @command, @command_opts = nil, nil - @block = block else raise ArgumentError, "Invalid only_if/not_if command: #{command.inspect} (#{command.class})" end end def continue? + # configure late in case guard_interpreter is specified on the resource after the conditional + configure + case @positivity when :only_if evaluate |