Unmunge is the reverse of munge. While munge allows the type to return a different parameter value or properties should than the one it was created with, unmunge does the reverse.
It can be used for instance to store a value in a different representation but still be able to return genuine value to the outside world. Signed-off-by: Brice Figureau <[email protected]> --- lib/puppet/parameter.rb | 16 +++++++++++++++- lib/puppet/property.rb | 4 ++-- spec/unit/parameter.rb | 10 ++++++++++ spec/unit/property.rb | 16 ++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/lib/puppet/parameter.rb b/lib/puppet/parameter.rb index 04b3aec..ae152de 100644 --- a/lib/puppet/parameter.rb +++ b/lib/puppet/parameter.rb @@ -286,6 +286,13 @@ class Puppet::Parameter define_method(:unsafe_munge, &block) end + # Does the parameter supports reverse munge? + # This will be called when something wants to access the parameter + # in a canonical form different to what the storage form is. + def unmunge(&block) + define_method(:unmunge, &block) + end + # Mark whether we're the namevar. def isnamevar @isnamevar = true @@ -446,6 +453,11 @@ class Puppet::Parameter self.class.value_collection.munge(value) end + # no unmunge by default + def unmunge(value) + value + end + # A wrapper around our munging that makes sure we raise useful exceptions. def munge(value) begin @@ -482,7 +494,9 @@ class Puppet::Parameter @resource = nil end - attr_reader :value + def value + unmunge(@value) + end # Store the value provided. All of the checking should possibly be # late-binding (e.g., users might not exist when the value is assigned diff --git a/lib/puppet/property.rb b/lib/puppet/property.rb index 3bb1a4f..a144f28 100644 --- a/lib/puppet/property.rb +++ b/lib/puppet/property.rb @@ -332,9 +332,9 @@ class Puppet::Property < Puppet::Parameter [self.class.name, @resource.name] end if match_all? - return @should + return @should.collect { |val| self.unmunge(val) } else - return @should[0] + return self.unmunge(@should[0]) end else return nil diff --git a/spec/unit/parameter.rb b/spec/unit/parameter.rb index 94f5cfd..42d3103 100755 --- a/spec/unit/parameter.rb +++ b/spec/unit/parameter.rb @@ -46,6 +46,16 @@ describe Puppet::Parameter do @parameter.value.should == "bar" end + it "should unmunge the value when accessing the actual value" do + @parameter.class.unmunge do |value| value.to_sym end + @parameter.value = "foo" + @parameter.value.should == :foo + end + + it "should return the actual value by default when unmunging" do + @parameter.unmunge("bar").should == "bar" + end + it "should return any set value" do @parameter.value = "foo" @parameter.value.should == "foo" diff --git a/spec/unit/property.rb b/spec/unit/property.rb index e2ba833..f09549d 100755 --- a/spec/unit/property.rb +++ b/spec/unit/property.rb @@ -129,6 +129,22 @@ describe Puppet::Property do it "should default to :first array_matching" do @class.array_matching.should == :first end + + it "should unmunge the returned value if :array_matching is set to :first" do + @property.class.unmunge do |v| v.to_sym end + @class.array_matching = :first + @property.should = %w{one two} + + @property.should.must == :one + end + + it "should unmunge all the returned values if :array_matching is set to :all" do + @property.class.unmunge do |v| v.to_sym end + @class.array_matching = :all + @property.should = %w{one two} + + @property.should.must == [:one, :two] + end end describe "when validating values" do -- 1.6.0.2 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
