更新:这是现在的首选方式:
action :create do
my_action_helper
end
action_class do
def my_action_helper
end
end
这些替代方案都有效:
action :create do
my_action_helper
end
action_class.class_eval do
def my_action_helper
end
end
和:
action :create do
my_action_helper
end
action_class.send(:define_method, :my_action_helper) do
end
在后一种情况下,如果您的方法上有 args 或块,则它是标准的 define_method 语法,示例:
# two args
action_class.send(:define_method, :my_action_helper) do |arg1, arg2|
# varargs and block
action_class.send(:define_method, :my_action_helper) do |*args, &block|
我们可能需要将一些 DSL 糖添加到自定义资源 API 中以使其更好。
(为此问题添加票证:https ://github.com/chef/chef/issues/4292 )