new_resource ref


#1

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.


#2

On Tuesday, July 1, 2014 at 8:39 AM, pmccallick@paraport.com 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


#3

Perfect, thanks

On 7/1/14, 8:48 AM, “Daniel DeLeo” dan@kallistec.com wrote:

On Tuesday, July 1, 2014 at 8:39 AM, pmccallick@paraport.com 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