Issue #6759 has been updated by Luke Kanies.

I agree with Daniel - I agree that the current system is non-optimal, but I 
think your recommendation will miss problems caused by providing invalid 
options and will also require a lot of extra code for every method.

I'd prefer to have a clean library that we used to provide consistent behavior. 
 I tried to do this with the Puppet::Util::MethodHelper module, but it ended up 
being uglier to use it than to just use the one-liner you're decrying.  My 
mistake was not trimming down the module and then using it consistently.
----------------------------------------
Refactor #6759: Improve internal option handling pattern
https://projects.puppetlabs.com/issues/6759

Author: Markus Roberts
Status: Accepted
Priority: Normal
Assignee: 
Category: 
Target version: 
Affected Puppet version: 
Keywords: 
Branch: 


The code base contains many occurrences of the pattern:

<pre>
      def initialize(..., options = {:op1 => 'default1'})
         options.each { |opt, val| send(opt.to_s + "=", val) } 
         :
</pre>

This is problematic for several reasons; it should be replaced with something 
like:

<pre>
      def initialize(..., options = {})
         options = {
            :opt1 => 'default',
            :opt2 => 'default2',
            :
          }.update options
         @opt1 = options.delete(:opt1)
         @opt2 = options.delete(:opt2)
         :
         raise "Unknown options: #{options.inspect}" unless options.empty?
         :
</pre>

If inheritance is an issue this can be augmented to:
 
(in the base)
<pre>
      def initialize(..., options = {})
         parse_options({
            :opt1 => 'default',
            :opt2 => 'default2',
            :
          }.update options)
         :

      def parse_options(options)
         @opt1 = options.delete(:opt1)
         @opt2 = options.delete(:opt2)
         :
         raise "Unknown options: #{options.inspect}" unless options.empty?
         :
</pre>

(in children)
<pre>
      def initialize(..., options = {})
         super(...,{
            :opt3 => 'default3',
            :opt4 => 'default4',
            :
          }.update options)
         :

      def parse_options(options)
        @opt3 = options.delete(:opt3)
        @opt4 = options.delete(:opt4)
        :
        super(options)
      end
</pre>
      






-- 
You have received this notification because you have either subscribed to it, 
or are involved in it.
To change your notification preferences, please click here: 
http://projects.puppetlabs.com/my/account

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Bugs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-bugs?hl=en.

Reply via email to