diff options
author | Bryan McLellan <btm@loftninjas.org> | 2014-09-25 16:29:19 -0400 |
---|---|---|
committer | Bryan McLellan <btm@loftninjas.org> | 2014-09-30 20:13:31 -0400 |
commit | 906b6cc2dd59c001955a6d8e7dc26296c6663539 (patch) | |
tree | 69de3a769fdf4b5f1c058f0d4a7aef2df1036b6e | |
parent | b04c9a5c515fb07317c00a3fbc9a657b58f26930 (diff) | |
download | chef-906b6cc2dd59c001955a6d8e7dc26296c6663539.tar.gz |
refactor conditional to evaluate for guards later
If we don't create the guard_interpreter until we're ready to test it,
we're sure to already have all the resource attributes evaluated.
Previously we set up the guard_interpreter upon initialization, that
is when it was first set on the resource.
-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 |