Chef infra 自定义资源中的私有方法不起作用
我想使用自定义资源执行类似的操作: 下面是有效的提供者代码(LWRP)Chef infra 自定义资源中的私有方法不起作用,chef-infra,lwrp,Chef Infra,Lwrp,我想使用自定义资源执行类似的操作: 下面是有效的提供者代码(LWRP) action :create my_private_method(a) my_private_method(b) end private def my_private_method(p) (some code) end 如果我使用自定义资源生成类似代码,并定义私有方法,则chef client运行将失败,并出现错误: No resource or method 'my_private_method' for
action :create
my_private_method(a)
my_private_method(b)
end
private
def my_private_method(p)
(some code)
end
如果我使用自定义资源生成类似代码,并定义私有方法,则chef client运行将失败,并出现错误:
No resource or method 'my_private_method' for 'LWRP resource ...
在自定义资源中声明/调用私有方法的语法是什么?
新建\u资源。发送(:my\u private\u method,a)
,因此不太建议将内容私有化。更新:现在这是首选方法:
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
以及:
在后一种情况下,如果方法上有args或块,则使用标准的define_方法语法,例如:
# 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|
我们可能需要在定制资源API中添加一些DSL糖,以使其更好
(本期新增门票:)谢谢,真难看。在普通LWRP中,它是常规方法调用。在普通LWRP中也一样,但请记住,您是在资源类上定义方法,而不是在提供程序上定义方法。
# 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|