summaryrefslogtreecommitdiff
path: root/lib/chef/resource
diff options
context:
space:
mode:
authorBryan McLellan <btm@loftninjas.org>2014-09-25 16:29:19 -0400
committerBryan McLellan <btm@loftninjas.org>2014-09-30 20:13:31 -0400
commit906b6cc2dd59c001955a6d8e7dc26296c6663539 (patch)
tree69de3a769fdf4b5f1c058f0d4a7aef2df1036b6e /lib/chef/resource
parentb04c9a5c515fb07317c00a3fbc9a657b58f26930 (diff)
downloadchef-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.
Diffstat (limited to 'lib/chef/resource')
-rw-r--r--lib/chef/resource/conditional.rb24
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