how to register a "respond_to?" ?

20 views
Skip to first unread message

Philip Brown

unread,
May 9, 2012, 4:55:54 PM5/9/12
to puppe...@googlegroups.com
I'm interested in the pattern of

if provider.respond_to?()


In particular,

provider.validate if provider.respond_to?(:validate)

I have

def validate
  blah blah

in my provider, but that does not seem to be enough.

Nick Lewis

unread,
May 9, 2012, 5:00:01 PM5/9/12
to puppe...@googlegroups.com
That *should* work. What respond_to? means is simply "does this object have a public method by this name?" What's the behavior you're seeing? Is it just not calling validate? And, importantly, are you certain it's not calling validate?

Beyond that, is the value of `provider` what you expect it to be? Do you have multiple providers, and perhaps the wrong one is being used? Where have you defined validate, and where are you calling it?

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/puppet-dev/-/XkJcdSByG5UJ.
To post to this group, send email to puppe...@googlegroups.com.
To unsubscribe from this group, send email to puppet-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.

Philip Brown

unread,
May 9, 2012, 5:21:38 PM5/9/12
to puppe...@googlegroups.com


On Wednesday, May 9, 2012 2:00:01 PM UTC-7, Nick Lewis wrote:
On Wednesday, May 9, 2012 at 1:55 PM, Philip Brown wrote:
I'm interested in the pattern of

if provider.respond_to?()


In particular,

provider.validate if provider.respond_to?(:validate)

I have

def validate
  blah blah

in my provider, but that does not seem to be enough.

That *should* work. What respond_to? means is simply "does this object have a public method by this name?" What's the behavior you're seeing? Is it just not calling validate? And, importantly, are you certain it's not calling validate?

Beyond that, is the value of `provider` what you expect it to be? Do you have multiple providers, and perhaps the wrong one is being used? Where have you defined validate, and where are you calling it?

ookay.. lots of questions, so you get lots of answers :)


I need a top level validate, not one under a newparam(). I need to ensure that multiple params are defined, rather than just some of them.

I'm confirming/denying function calling, with big obnoxious lines such as
 Puppet.debug("   #### function called here")

1. if I define it in the "type" file, as
  validate do
    unless self[:fmri] and self[:property] and self[:value]
      raise ArgumentError,
            "svcprop must have fmri and property and value"
    end
  end

it works... BUT.. I want to allow multiple providers, and let them pick which arguments are required

2. If I have a  type-file definition. that invokes provider.validate, and then in the provider file,

  def validate
     Puppet.debug(" #### Called")
  end


It gets called fine.
(What is the difference between "def validate", and "validate do", btw?! )



3. If I have no  type-file definition.  but just one in the provider file, similar to above,
it doesnt get called.


I've seen another type (file.rb), call

    provider.validate if provider.respond_to?(:validate)


but I dont see its providers doing anything beyond

"def validate
   ..."

So, I'm baffled.






 

Nick Lewis

unread,
May 9, 2012, 5:45:46 PM5/9/12
to puppe...@googlegroups.com
On Wednesday, May 9, 2012 at 2:21 PM, Philip Brown wrote:

ookay.. lots of questions, so you get lots of answers :)

I figured it would save us both time if none of my questions are helpful. :) 

I need a top level validate, not one under a newparam(). I need to ensure that multiple params are defined, rather than just some of them.

I'm confirming/denying function calling, with big obnoxious lines such as
 Puppet.debug("   #### function called here")

1. if I define it in the "type" file, as
  validate do
    unless self[:fmri] and self[:property] and self[:value]
      raise ArgumentError,
            "svcprop must have fmri and property and value"
    end
  end

it works... BUT.. I want to allow multiple providers, and let them pick which arguments are required

2. If I have a  type-file definition. that invokes provider.validate, and then in the provider file,

I'm not sure you mean when you say "type-file definition". Could you clarify?

If you can/are willing to share, it would help to be able to see your type/provider code.

Here's a gist with a minimal implementation of this functionality, which hopefully will make it clear where your code differs (and if it really doesn't differ, we have a truly fascinating mystery on our hands!):

  def validate
     Puppet.debug(" #### Called")
  end


It gets called fine.
(What is the difference between "def validate", and "validate do", btw?! )

 `validate do` is basically just a convention to do something like `validate if respond_to? :validate`. It defines a validation block, which is then used implicitly by Puppet as part of applying the catalog. It could also have been implemented by having Puppet call any given "validate" method, if one is provided. I suppose the reasoning was that this way seems less like magic and more like an obvious part of the type API; I'm not entirely sure.

3. If I have no  type-file definition.  but just one in the provider file, similar to above,
it doesnt get called.


I've seen another type (file.rb), call

    provider.validate if provider.respond_to?(:validate)


but I dont see its providers doing anything beyond

"def validate
   ..."

So, I'm baffled.






 

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/puppet-dev/-/uJqNViZ_QZcJ.

Philip Brown

unread,
May 10, 2012, 2:20:47 PM5/10/12
to puppe...@googlegroups.com


On Wednesday, May 9, 2012 2:45:46 PM UTC-7, Nick Lewis wrote:

Here's a gist with a minimal implementation of this functionality, which hopefully will make it clear where your code differs (and if it really doesn't differ, we have a truly fascinating mystery on our hands!):

Hmm. well, i threw together a stupidly stripped down version, and it worked.. so then I went back to my real code, ... and I notice it works now too :-/
maybe I messed up the debugging.
Thanks for the reply confirming that it's supposed to work that way :)



 `validate do` is basically just a convention to do something like `validate if respond_to? :validate`. It defines a validation block, which is then used implicitly by Puppet as part of applying the catalog. It could also have been implemented by having Puppet call any given "validate" method, if one is provided. I suppose the reasoning was that this way seems less like magic and more like an obvious part of the type API; I'm not entirely sure.

Hmm. that seems like somewhat circular definition there. I'm a bit lost. but oh well, it works now :-}

Its too bad that the "validate" code doesnt trigger for the "puppet parser validate" phase, though. I thought it would. But it doesnt appear to.


Reply all
Reply to author
Forward
0 new messages