W dniu poniedziałek, 27 stycznia 2014 10:14:40 UTC+1 użytkownik Erik Dalén
napisał:
>
> On 27 January 2014 04:00, Pawel Tomulik <[email protected]<javascript:>
> > wrote:
>
>> Hi,
>>
>> macros in puppets -> http://forge.puppetlabs.com/ptomulik/macro
>>
>> They're similar to parser functions with three differences:
>>
>> - macro names may resemble names of puppet variables/bindings, e.g.
>> 'foo::bar::geez'
>> - stored in a hierarchy of directories, that is (for macro
>> 'foo::bar::geez'):
>>
>> + lib/puppet/parser/macros/
>> + foo/
>> + bar/
>> + geez.rb
>>
>> - they're a little bit easier to implement than functions, because
>> arity checking is provided out of the box
>>
>> While I think that block parameters is nicer than the current puppet
> method of putting all arguments in an array, there is builtin support for
> arity checking in puppet 3.x. You specify the arity in the function
> definition, like :arity => 2.
>
I suppose, It's not possible to specify default parameters that way, this
was reason for introducing 'parameters' method to Object in ruby 1.9.
Macros handle default parameters out of the box (on ruby 1.9+) and you
don't have to specify arities/parameter descriptions explicitly. Well, this
could be easily implemented also for functions, I believe.
>
> I think it should be possible for you to do strict arity checking even
> with just regular blocks as well, not only with lambdas. The block
> parameter has a arity method that you can use to find out the arity and
> then have something that runs before the macro in question that checks the
> parameters given against that, kind of like how the puppet 3.x function
> calling does it.
>
Of course it's possible, maybe not by using arity, but the parameters
method (1.9+). It's actually implemented in macros and work seamlesly for
lambdas and blocks on ruby 1.9+ (I was quite asleep when writing docs). The
arity method (which is the only available method on ruby 1.8) doesn't
handle well default parameters:
irb(main):020:0> def arity(&block); p block.arity; end
irb(main):021:0> arity do |x,y=1| end
1
=> 1
irb(main):021:0> arity do |x| end
1
=> 1
With lambdas, we just have the arities verified additionally by ruby, so in
case my macro's validation lies, we always have this extra guard.
>
>
>> In some cases these macros may be more handy than 'params' classes when
>> defining defaults for parameters. For example defaults for defined types
>> which vary from instance to instance, or values which are hard to be
>> computed in puppet DSL may be easily handled with macros.
>>
>> Leave a comment, if you find it useful or useless.
>>
>
> Cool stuff. I like the simpler definition of them with less boilerplate
> than the current functions.
>
> One thought though when me and others and discussed a new function API for
> puppet we considered just having a module with methods on it instead. So to
> define a new function you would just need to open that module and define a
> new method. I think that would be more straightforward for people that
> already know Ruby. And probably a lot easier to write unit tests for as
> they would work just like a unit test for any other method. What do you
> think about doing something like that to define functions/macros?
>
>
>
That looks ok. I suppose it would need some convention for method naming,
e.g:
module Functions
def function_foo() # <- this is puppet function
end
def foo() <-this is not
end
end
You could also make a convention for documentation:
module Functions
def doc_foo <<-EOT
blah blah
end
end
I didn't thought about it this way, but it looks fine. I'm not sure,
however, how could you handle environments in this approach. I've seen that
currently functions are stored in hashes, one hash per puppet environment.
In my case, I just keep a hash of Procs for each environment. I also plan
to have another hash of macro docs, which will be build only on demand.
>
> --
> Erik Dalén
>
--
You received this message because you are subscribed to the Google Groups
"Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/puppet-dev/3f4a07cf-f6dc-409b-96e7-85c1d0a1b6de%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.