diff options
author | Markus Roberts <Markus@reality.com> | 2009-09-04 13:00:30 -0700 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-09-05 09:16:29 +1000 |
commit | 4aa7fcec3d05a45c3a9d8b76fd9892ae28e7618b (patch) | |
tree | 1110f47b63dbbace932f2a305d5b8d3b21b40f74 /lib | |
parent | 1f6c74dbdfe8e0d27f74dd08c474747d24fafb61 (diff) | |
download | puppet-4aa7fcec3d05a45c3a9d8b76fd9892ae28e7618b.tar.gz puppet-4aa7fcec3d05a45c3a9d8b76fd9892ae28e7618b.tar.xz puppet-4aa7fcec3d05a45c3a9d8b76fd9892ae28e7618b.zip |
Monkey patch to improve yaml compatibility between ruby versions
Ruby 1.8.1 can not parse the yanl produced by later versions because
it requires explict type tagging of symbols. This patch adds the
tagging on to later versions so that mixed version instalations can
use yaml.
Signed-off-by: Markus Roberts <Markus@reality.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/util.rb | 1 | ||||
-rw-r--r-- | lib/puppet/util/monkey_patches.rb | 43 |
2 files changed, 44 insertions, 0 deletions
diff --git a/lib/puppet/util.rb b/lib/puppet/util.rb index a81e321bc..e1e699286 100644 --- a/lib/puppet/util.rb +++ b/lib/puppet/util.rb @@ -1,5 +1,6 @@ # A module to collect utility functions. +require 'puppet/util/monkey_patches' require 'sync' require 'puppet/external/lock' diff --git a/lib/puppet/util/monkey_patches.rb b/lib/puppet/util/monkey_patches.rb new file mode 100644 index 000000000..817b813f0 --- /dev/null +++ b/lib/puppet/util/monkey_patches.rb @@ -0,0 +1,43 @@ +# +# Monkey patches to ruby classes for compatibility +# +# +# In earlier versions of ruby (e.g. 1.8.1) yaml serialized symbols with an explicit +# type designation. Later versions understand the explicit form in addition to the +# implicit "literal" form (e.g. :symbol) which they produce. +# +# This causes problems when the puppet master and the client are running on different +# versions of ruby; the newer version can produce yaml that it's older partner can't +# decypher. +# +# This patch causes newer versions to produce the older encoding for Symbols. It is +# only applied if the existing library does not already produce them. Thus it will +# not be applied on older rubys and it will not be applied more than once. It also +# checks that it has been applied to a version which support it and, if not reverts +# to the original. +# +require "yaml" + +if :test.to_yaml !~ %r{!ruby/sym} + class Symbol + if !respond_to? :original_to_yaml + alias :original_to_yaml :to_yaml + def to_yaml(opts={}) + YAML::quick_emit(nil,opts) { |out| + if out.respond_to? :scalar + # 1.8.5 through 1.8.8, possibly others + out.scalar("tag:ruby:sym", to_s,:to_yaml_style) + elsif out.respond_to? :<< + # 1.8.2, possibly others + out << "!ruby/sym " + self.id2name.to_yaml( :Emitter => out ) + else + # go back to the base version if neither of the above work + alias :to_yaml :original_to_yaml + to_yaml(opts) + end + } + end + end + end +end |