On Feb 1, 2011, at 6:54 PM, Nan Liu wrote:

> Includes following changes:
> 
> Fixed puppet resource so it have trailing , for last attribute
> Added logic to align =>
> Sort attribute by name
> Use 2 spaces instead of 4
> 
> ---
> lib/puppet/resource.rb |   11 ++++++++---
> 1 files changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/lib/puppet/resource.rb b/lib/puppet/resource.rb
> index e832804..0419cdd 100644
> --- a/lib/puppet/resource.rb
> +++ b/lib/puppet/resource.rb
> @@ -255,14 +255,19 @@ class Puppet::Resource
> 
>   # Convert our resource to Puppet code.
>   def to_manifest
> +    # Collect list of attributes to align =>
> +    attributes = @parameters.collect { |attr, val| attr }

This could be done more easily with '@parameters.keys'.

> +    attr_max = attributes.max{ |a, b| a.to_s.length <=> b.to_s.length 
> }.to_s.length

This is a bit gratuitous, but this is at least *shorter* this way:

  attr_max = attributes.inject(0) { |m,a| a.length > m ? a.length : m }

But 'inject' is probably always more confusing. :)  (Note that I haven't tested 
this, but it should work.)

> +
>     "%s { '%s':\n%s\n}" % [self.type.to_s.downcase, self.title,
>       @parameters.collect { |p, v|
> +        space = " " * (attr_max-p.to_s.length)
>         if v.is_a? Array
> -          "    #{p} => [\'#{v.join("','")}\']"
> +          "  #{p}#{space} => [\'#{v.join("','")}\']"
>         else
> -          "    #{p} => \'#{v}\'"
> +          "  #{p}#{space} => \'#{v}\'"

This could be done more easily with '%5s' or something similar.  You probably 
need to make a template:

  template = "%#{attr_max + 1}s => '%s'"

And then use it in the loop:

  template % [p, v]

You'd need two templates, or a partial template that you then fill in with the 
values (because of the arrays), but it should be relatively straightforward.

>         end
> -      }.join(",\n")
> +      }.sort.join(",\n") + ","
>       ]
>   end
> 
> -- 
> 1.7.3.5
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Puppet Developers" 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-dev?hl=en.
> 


-- 
It is well to remember that the entire universe, with one trifling
exception, is composed of others.     --John Andrew Holmes
---------------------------------------------------------------------
Luke Kanies  -|-   http://puppetlabs.com   -|-   +1(615)594-8199




-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" 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-dev?hl=en.

Reply via email to