节点模式,以匹配开始中的某些节点,但不是所有节点



我很难写一个单节点匹配来检测这种情况:

chef_gem 'deep_merge' do
action :nothing
compile_time true
end.run_action(:install)

我最初写这个图案

def_node_matcher :compile_time_and_run_action?, <<-PATTERN
(send
$(block
(send nil? ... )
(args)
(begin
(send nil? :action (sym $_) )
(send nil? :compile_time (true) )
)
) :run_action (sym $_) )
PATTERN

问题是action方法和compile_time方法可以是任何顺序,并且在那里的begin中可能有其他方法:

chef_gem 'deep_merge' do
compile_time true
action :nothing
foo bar
end.run_action(:install)

在过去,我使用<gt;在数组和散列中,但这似乎一开始就不起作用,所以这样的东西不会削减它:

def_node_matcher :compile_time_and_run_action?, <<-PATTERN
(send
$(block
(send nil? ... )
(args)
<(begin
...
(send nil? :action (sym $_) )
(send nil? :compile_time (true) )
)>
) :run_action (sym $_) )
PATTERN

有没有一种很好的方法可以在我错过的一场比赛中做到这一点?

-Tim

使用<>是可行的方法,但对于begin的子代使用此方法。我认为这应该有效:

def_node_matcher :compile_time_and_run_action?, <<-PATTERN
(send
$(block
(send nil? ... )
(args)
(begin <
(send nil? :action (sym $_) )
(send nil? :compile_time (true) )
...
>)
) :run_action (sym $_) )
PATTERN