summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-07-30 09:39:51 -0600
committerJohn Keiser <john@johnkeiser.com>2015-07-30 09:52:42 -0600
commit221e63a2de6414ae1d4422753589dccf7d35c351 (patch)
treedfd50c9ca41c605a60f63ce1b11b2e4a631d7963 /lib
parent96053ccb88f145a1f6a354cfe709bc99176723cb (diff)
downloadchef-221e63a2de6414ae1d4422753589dccf7d35c351.tar.gz
Make Resource.action work with non-standard namesjk/action_dash
Diffstat (limited to 'lib')
-rw-r--r--lib/chef/provider.rb35
1 files changed, 26 insertions, 9 deletions
diff --git a/lib/chef/provider.rb b/lib/chef/provider.rb
index 43d58740f2..089c28be50 100644
--- a/lib/chef/provider.rb
+++ b/lib/chef/provider.rb
@@ -297,17 +297,34 @@ class Chef
# compile the resources, converging them, and then checking if any
# were updated (and updating new-resource if so)
def action(name, &block)
- class_eval <<-EOM, __FILE__, __LINE__+1
- def action_#{name}
- return_value = compile_action_#{name}
- Chef::Runner.new(run_context).converge
- return_value
- ensure
- if run_context.resource_collection.any? {|r| r.updated? }
- new_resource.updated_by_last_action(true)
+ # We first try to create the method using "def method_name", which is
+ # preferred because it actually shows up in stack traces. If that
+ # fails, we try define_method.
+ begin
+ class_eval <<-EOM, __FILE__, __LINE__+1
+ def action_#{name}
+ return_value = compile_action_#{name}
+ Chef::Runner.new(run_context).converge
+ return_value
+ ensure
+ if run_context.resource_collection.any? {|r| r.updated? }
+ new_resource.updated_by_last_action(true)
+ end
+ end
+ EOM
+ rescue SyntaxError
+ define_method("action_#{name}") do
+ begin
+ return_value = send("compile_action_#{name}")
+ Chef::Runner.new(run_context).converge
+ return_value
+ ensure
+ if run_context.resource_collection.any? {|r| r.updated? }
+ new_resource.updated_by_last_action(true)
+ end
end
end
- EOM
+ end
# We put the action in its own method so that super() works.
define_method("compile_action_#{name}", &block)
end