- From: Daniel DeLeo <
>
- To:
- Subject: [chef] Re: new_resource ref
- Date: Tue, 1 Jul 2014 08:48:07 -0700
On Tuesday, July 1, 2014 at 8:39 AM,
wrote:
>
I'm creating an lwrp and in the provider i'm creating a template. inside the
>
template block @new_resource returns nil but new_resource gets a ref.
>
outside
>
the block either works. why?
>
>
I want to get a better understanding of what is going on.
The blocks you pass to a resource declaration are evaluated using
`instance_eval`, which changes the context in which the code is evaluated to
be “inside” the resource object. In the normal recipe case, you have:
# `self` here is a Chef::Recipe object
template(“name”) do
# `self` here is a Chef::Resource::Template object
end
To make this a little nicer in the LWRP case, resources in LWRPs have a
reference to their parent object (your provider class) and they forward any
unknown methods (in your case `new_resource`) to the parent.
This is a pretty in-depth explanation of how ‘self’ in ruby gets manipulated
by various kinds of `eval`, if you want to learn more:
http://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/
HTH,
--
Daniel DeLeo
Archive powered by MHonArc 2.6.16.