2011-06-29 20:30:46 -04:00
|
|
|
##
|
|
|
|
# The YAML module allows you to use one of the two YAML engines that ship with
|
|
|
|
# ruby. By default Psych is used but the old and unmaintained Syck may be
|
|
|
|
# chosen.
|
|
|
|
#
|
|
|
|
# See Psych or Syck for usage and documentation.
|
|
|
|
#
|
|
|
|
# To set the YAML engine to syck:
|
|
|
|
#
|
|
|
|
# YAML::ENGINE.yamler = 'syck'
|
|
|
|
#
|
|
|
|
# To set the YAML engine back to psych:
|
|
|
|
#
|
|
|
|
# YAML::ENGINE.yamler = 'psych'
|
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
module YAML
|
2010-04-03 17:50:47 -04:00
|
|
|
class EngineManager # :nodoc:
|
|
|
|
attr_reader :yamler
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2010-04-03 17:50:47 -04:00
|
|
|
def initialize
|
|
|
|
@yamler = nil
|
2004-07-15 01:04:49 -04:00
|
|
|
end
|
|
|
|
|
2010-04-03 17:50:47 -04:00
|
|
|
def syck?
|
|
|
|
'syck' == @yamler
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
|
2010-04-03 17:50:47 -04:00
|
|
|
def yamler= engine
|
|
|
|
raise(ArgumentError, "bad engine") unless %w{syck psych}.include?(engine)
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2011-06-12 23:55:00 -04:00
|
|
|
require engine unless (engine == 'syck' ? Syck : Psych).const_defined?(:VERSION)
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2011-09-07 04:28:42 -04:00
|
|
|
::Object.class_eval <<-eorb, __FILE__, __LINE__ + 1
|
2010-04-03 17:50:47 -04:00
|
|
|
remove_const 'YAML'
|
|
|
|
YAML = #{engine.capitalize}
|
|
|
|
remove_method :to_yaml
|
|
|
|
alias :to_yaml :#{engine}_to_yaml
|
|
|
|
eorb
|
2010-04-07 22:32:06 -04:00
|
|
|
|
|
|
|
@yamler = engine
|
2010-04-03 17:50:47 -04:00
|
|
|
engine
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
2010-04-03 17:50:47 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2011-06-29 20:30:46 -04:00
|
|
|
##
|
|
|
|
# Allows changing the current YAML engine. See YAML for details.
|
|
|
|
|
2010-04-03 17:50:47 -04:00
|
|
|
ENGINE = YAML::EngineManager.new
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
|
2011-06-12 23:55:00 -04:00
|
|
|
if defined?(Psych)
|
|
|
|
engine = 'psych'
|
|
|
|
elsif defined?(Syck)
|
|
|
|
engine = 'syck'
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
require 'psych'
|
|
|
|
engine = 'psych'
|
|
|
|
rescue LoadError
|
|
|
|
warn "#{caller[0]}:"
|
|
|
|
warn "It seems your ruby installation is missing psych (for YAML output)."
|
|
|
|
warn "To eliminate this warning, please install libyaml and reinstall your ruby."
|
|
|
|
require 'syck'
|
|
|
|
engine = 'syck'
|
|
|
|
end
|
2011-05-23 15:05:47 -04:00
|
|
|
end
|
|
|
|
|
2010-04-03 17:50:47 -04:00
|
|
|
module Syck
|
|
|
|
ENGINE = YAML::ENGINE
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
|
2010-04-03 17:50:47 -04:00
|
|
|
module Psych
|
|
|
|
ENGINE = YAML::ENGINE
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2010-04-07 22:45:23 -04:00
|
|
|
YAML::ENGINE.yamler = engine
|