On Thu, Apr 17, 2014 at 7:27 AM, Jakov Sosic <[email protected]> wrote:
> Hi, I'm forwarding my question from puppet-users because it didn't get > many answers there... > > > > I'm developing some of my custom types, and some of them share same > params, with exactly the same methods... > > For example, I have these params in three of my types and they are same: > > lib/puppet/type/mytype1.rb > lib/puppet/type/mytype2.rb > lib/puppet/type/mytype3.rb > > newproperty(:comment) do > defaultto '' > end > > > Is there a way I can maybe extract that code into some external library > and reuse it in all of my three types? Now, this definition is not a > problem, but when you start to include various checks, and override methods > like insync? and your param code grows to 30-40 lines, and you use it in > multiple types, it gets usefull to extract that code to some external class > and include it into these types. > You can write a custom property to change how it behaves (by writing a custom insync, should_to_s, is_to_s)[1]. But I don't think there's a way to share newproperty, newparam across multiple types. There are examples of auto generated types[2], but there's so much metaprogramming I doubt it's worth while for a simple resource. > I have same problem with methods in providers, for example: > > lib/puppet/provider/mytype1/default.rb > lib/puppet/provider/mytype2/default.rb > lib/puppet/provider/mytype3/default.rb > > all have for example: > > # sets comment > def comment=(value) > @property_hash[:comment]=(value) > end > > Is there a way to extract method provider into some external file and just > include it somehow? > > > If it's possible I would like to escape a route of creating another custom > type, and add its value to param of another custom type :-/ The easiest thing to do for providers is have the common code in a parent provider[3]. You can also override the method in the child provider. Nan 1. https://github.com/vmware/vmware-vmware_lib/blob/master/lib/puppet/property/vmware_array.rb 2. https://github.com/vmware/vmware-vcenter/blob/master/lib/puppet/type/esx_vmknic.rb#L51-L92 3. parent: https://github.com/vmware/vmware-vcenter/blob/master/lib/puppet/provider/vcenter.rb Puppet::Type.type(...).provide(:default, :parent => Puppet::Provider::Vcenter) do -- 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/CACqVBqCM%3DzL9r4h-d8%2BFRcsGc0MTF--eDYAkGER1Enbp7Rq9iQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
