Reputation: 13234
cookbook/libraries/rn_helper.rb:
def sample_func
puts "woohoo"
end
cookbook/resources/rn.rb:
action :create do
sample_func
end
The above code works perfectly. The below code does not:
cookbook/libraries/rn_helper.rb:
module SampleModule
def sample_func
puts "woohoo"
end
end
cookbook/resources/rn.rb:
extend SampleModule
action :create do
sample_func
end
The error is as follows:
NameError: custom resource[usr.bin.foo] had an error: NameError: No resource, method, or local variable named 'sample_func' for 'LWRP resource some_resource from cookbook some_cookbook action provider "usr.bin.foo"'
This is a new-style, Chef 12.5 custom resource -- there is no provider file. Everything is described in the resource itself.
How can I "modularize" my helpers using the new-style custom resource syntax?
Upvotes: 2
Views: 720
Reputation: 54211
You can access the internal "action class" to add methods:
action_class do
include SampleModule
end
Upvotes: 1
Reputation: 151
You need to extend within the action block. When passing a block in like this, a different class than the top-level one is being yielded. Because you are using the function within block that was yielded that class, you need to extend that class to include the methods you need.
You should be able to do that by adding extend SampleModule
as the first line within the action block.
Upvotes: 0