diff --git a/lib/puppet/util/rails/reference_serializer.rb b/lib/puppet/util/rails/reference_serializer.rb index 8c4f1aaff..63f109cec 100644 --- a/lib/puppet/util/rails/reference_serializer.rb +++ b/lib/puppet/util/rails/reference_serializer.rb @@ -1,18 +1,32 @@ module Puppet::Util::ReferenceSerializer def unserialize_value(val) - if val =~ /^--- [!:]/ + case val + when /^--- / YAML.load(val) + when "true" + true + when "false" + false else val end end def serialize_value(val) - if val.is_a?(Puppet::Parser::Resource::Reference) + case val + when Puppet::Parser::Resource::Reference YAML.dump(val) + when true, false + # The database does this for us, but I prefer the + # methods be their exact inverses. + # Note that this means quoted booleans get returned + # as actual booleans, but there doesn't appear to be + # a way to fix that while keeping the ability to + # search for parameters set to true. + val.to_s else val end end -end \ No newline at end of file +end diff --git a/spec/unit/util/reference_serializer.rb b/spec/unit/util/reference_serializer.rb new file mode 100644 index 000000000..c3da45a36 --- /dev/null +++ b/spec/unit/util/reference_serializer.rb @@ -0,0 +1,52 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' +require 'puppet/util/rails/reference_serializer' + +class SerializeTester + include Puppet::Util::ReferenceSerializer +end + +describe Puppet::Util::ReferenceSerializer do + before do + @tester = SerializeTester.new + end + + describe "when serializing" do + it "should yaml-dump resource references" do + ref = Puppet::Parser::Resource::Reference.new(:type => "file", :title => "/foo") + @tester.serialize_value(ref).should =~ /^---/ + end + + it "should convert the boolean 'true' into the string 'true'" do + @tester.serialize_value(true).should == "true" + end + + it "should convert the boolean 'false' into the string 'false'" do + @tester.serialize_value(false).should == "false" + end + + it "should return all other values" do + @tester.serialize_value("foo").should == "foo" + end + end + + describe "when unserializing" do + it "should yaml-load values that look like yaml" do + yaml = YAML.dump(%w{a b c}) + @tester.unserialize_value(yaml).should == %w{a b c} + end + + it "should convert the string 'true' into the boolean 'true'" do + @tester.unserialize_value("true").should == true + end + + it "should convert the string 'false' into the boolean 'false'" do + @tester.unserialize_value("false").should == false + end + + it "should return all other values" do + @tester.unserialize_value("foo").should == "foo" + end + end +end