2003-12-19 21:40:15 -05:00
|
|
|
# -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- vim: sw=4 ts=4
|
2003-05-09 17:25:50 -04:00
|
|
|
require 'date'
|
2005-09-12 23:58:33 -04:00
|
|
|
|
2003-05-22 01:41:06 -04:00
|
|
|
class Class
|
|
|
|
def to_yaml( opts = {} )
|
2004-05-06 02:29:56 -04:00
|
|
|
raise TypeError, "can't dump anonymous class %s" % self.class
|
2003-05-22 01:41:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
class Object
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:ruby.yaml.org,2002:object"
|
|
|
|
def to_yaml_style; end
|
|
|
|
def to_yaml_properties; instance_variables.sort; end
|
2003-05-09 17:25:50 -04:00
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( object_id, opts ) do |out|
|
|
|
|
out.map( taguri, to_yaml_style ) do |map|
|
|
|
|
to_yaml_properties.each do |m|
|
2003-12-19 21:40:15 -05:00
|
|
|
map.add( m[1..-1], instance_variable_get( m ) )
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Hash
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:ruby.yaml.org,2002:hash"
|
|
|
|
yaml_as "tag:yaml.org,2002:map"
|
|
|
|
def yaml_initialize( tag, val )
|
|
|
|
if Array === val
|
|
|
|
update Hash.[]( *val ) # Convert the map to a sequence
|
|
|
|
elsif Hash === val
|
|
|
|
update val
|
2003-05-09 17:25:50 -04:00
|
|
|
else
|
2005-09-12 23:58:33 -04:00
|
|
|
raise YAML::TypeError, "Invalid map explicitly tagged #{ tag }: " + val.inspect
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( object_id, opts ) do |out|
|
|
|
|
out.map( taguri, to_yaml_style ) do |map|
|
|
|
|
each do |k, v|
|
|
|
|
map.add( k, v )
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Struct
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:ruby.yaml.org,2002:struct"
|
|
|
|
def self.yaml_tag_class_name; self.name.gsub( "Struct::", "" ); end
|
|
|
|
def self.yaml_tag_read_class( name ); "Struct::#{ name }"; end
|
|
|
|
def self.yaml_new( klass, tag, val )
|
|
|
|
if Hash === val
|
|
|
|
struct_type = nil
|
|
|
|
|
|
|
|
#
|
|
|
|
# Use existing Struct if it exists
|
|
|
|
#
|
|
|
|
props = {}
|
|
|
|
val.delete_if { |k,v| props[k] = v if k =~ /^@/ }
|
|
|
|
begin
|
|
|
|
struct_name, struct_type = YAML.read_type_class( tag, Struct )
|
|
|
|
rescue NameError
|
|
|
|
end
|
|
|
|
if not struct_type
|
|
|
|
struct_def = [ tag.split( ':', 4 ).last ]
|
|
|
|
struct_type = Struct.new( *struct_def.concat( val.keys.collect { |k| k.intern } ) )
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Set the Struct properties
|
|
|
|
#
|
|
|
|
st = YAML::object_maker( struct_type, {} )
|
|
|
|
st.members.each do |m|
|
|
|
|
st.send( "#{m}=", val[m] )
|
|
|
|
end
|
|
|
|
props.each do |k,v|
|
|
|
|
st.instance_variable_set(k, v)
|
|
|
|
end
|
|
|
|
st
|
|
|
|
else
|
|
|
|
raise YAML::TypeError, "Invalid Ruby Struct: " + val.inspect
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( object_id, opts ) do |out|
|
2003-05-09 17:25:50 -04:00
|
|
|
#
|
|
|
|
# Basic struct is passed as a YAML map
|
|
|
|
#
|
2005-09-12 23:58:33 -04:00
|
|
|
out.map( taguri, to_yaml_style ) do |map|
|
|
|
|
self.members.each do |m|
|
2003-05-09 17:25:50 -04:00
|
|
|
map.add( m, self[m] )
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
|
|
|
self.to_yaml_properties.each do |m|
|
2004-05-06 02:29:56 -04:00
|
|
|
map.add( m, instance_variable_get( m ) )
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Array
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:ruby.yaml.org,2002:array"
|
|
|
|
yaml_as "tag:yaml.org,2002:seq"
|
|
|
|
def yaml_initialize( tag, val ); concat( val.to_a ); end
|
2003-05-09 17:25:50 -04:00
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( object_id, opts ) do |out|
|
|
|
|
out.seq( taguri, to_yaml_style ) do |seq|
|
|
|
|
each do |x|
|
|
|
|
seq.add( x )
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-05-22 13:56:30 -04:00
|
|
|
class Exception
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:ruby.yaml.org,2002:exception"
|
|
|
|
def Exception.yaml_new( klass, tag, val )
|
|
|
|
o = YAML.object_maker( klass, { 'mesg' => val.delete( 'message' ) } )
|
|
|
|
val.each_pair do |k,v|
|
|
|
|
o.instance_variable_set("@#{k}", v)
|
|
|
|
end
|
|
|
|
o
|
2003-05-22 13:56:30 -04:00
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( object_id, opts ) do |out|
|
|
|
|
out.map( taguri, to_yaml_style ) do |map|
|
|
|
|
map.add( 'message', message )
|
|
|
|
to_yaml_properties.each do |m|
|
2003-12-19 21:40:15 -05:00
|
|
|
map.add( m[1..-1], instance_variable_get( m ) )
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2003-05-22 13:56:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
class String
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:ruby.yaml.org,2002:string"
|
|
|
|
yaml_as "tag:yaml.org,2002:str"
|
2003-05-09 17:25:50 -04:00
|
|
|
def is_complex_yaml?
|
2005-09-12 23:58:33 -04:00
|
|
|
to_yaml_style or not to_yaml_properties.empty? or self =~ /\n.+/
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
def is_binary_data?
|
2005-09-12 23:58:33 -04:00
|
|
|
( self.count( "^ -~", "^\r\n" ) / self.size > 0.3 || self.count( "\x00" ) > 0 ) unless empty?
|
2004-05-06 02:29:56 -04:00
|
|
|
end
|
2005-09-12 23:58:33 -04:00
|
|
|
def String.yaml_new( klass, tag, val )
|
|
|
|
val = { 'str' => val } if String === val
|
|
|
|
if Hash === val
|
|
|
|
s = klass.allocate
|
|
|
|
# Thank you, NaHi
|
|
|
|
String.instance_method(:initialize).
|
|
|
|
bind(s).
|
|
|
|
call( val.delete( 'str' ) )
|
|
|
|
val.each { |k,v| s.instance_variable_set( k, v ) }
|
|
|
|
s
|
|
|
|
else
|
|
|
|
raise YAML::TypeError, "Invalid String: " + val.inspect
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( is_complex_yaml? ? object_id : nil, opts ) do |out|
|
|
|
|
if is_binary_data?
|
|
|
|
out.scalar( "tag:yaml.org,2002:binary", [self].pack("m"), :literal )
|
|
|
|
elsif to_yaml_properties.empty?
|
|
|
|
out.scalar( taguri, self, to_yaml_style )
|
2004-08-18 16:42:09 -04:00
|
|
|
else
|
2005-09-12 23:58:33 -04:00
|
|
|
out.map( taguri, to_yaml_style ) do |map|
|
|
|
|
map.add( 'str', "#{self}" )
|
|
|
|
to_yaml_properties.each do |m|
|
|
|
|
map.add( m, instance_variable_get( m ) )
|
|
|
|
end
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
2005-09-17 13:22:49 -04:00
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
class Symbol
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:ruby.yaml.org,2002:symbol"
|
|
|
|
yaml_as "tag:ruby.yaml.org,2002:sym"
|
|
|
|
# yaml_implicit /^:/, :yaml_new
|
|
|
|
def Symbol.yaml_new( klass, tag, val )
|
|
|
|
if String === val
|
|
|
|
val.intern
|
|
|
|
else
|
|
|
|
raise YAML::TypeError, "Invalid Symbol: " + val.inspect
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( nil, opts ) do |out|
|
2005-09-17 13:22:49 -04:00
|
|
|
out.scalar( "tag:yaml.org,2002:str", self.inspect, :plain )
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Range
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:ruby.yaml.org,2002:range"
|
|
|
|
def Range.yaml_new( klass, tag, val )
|
|
|
|
inr = %r'(\w+|[+-]?\d+(?:\.\d+)?(?:e[+-]\d+)?|"(?:[^\\"]|\\.)*")'
|
|
|
|
opts = {}
|
|
|
|
if String === val and val =~ /^#{inr}(\.{2,3})#{inr}$/o
|
|
|
|
r1, rdots, r2 = $1, $2, $3
|
|
|
|
opts = {
|
|
|
|
'begin' => YAML.load( "--- #{r1}" ),
|
|
|
|
'end' => YAML.load( "--- #{r2}" ),
|
|
|
|
'excl' => rdots.length == 3
|
|
|
|
}
|
|
|
|
val = {}
|
|
|
|
elsif Hash === val
|
|
|
|
opts['begin'] = val.delete('begin')
|
|
|
|
opts['end'] = val.delete('end')
|
|
|
|
opts['excl'] = val.delete('excl')
|
|
|
|
end
|
|
|
|
if Hash === opts
|
|
|
|
r = YAML::object_maker( klass, {} )
|
|
|
|
# Thank you, NaHi
|
|
|
|
Range.instance_method(:initialize).
|
|
|
|
bind(r).
|
|
|
|
call( opts['begin'], opts['end'], opts['excl'] )
|
|
|
|
val.each { |k,v| r.instance_variable_set( k, v ) }
|
|
|
|
r
|
|
|
|
else
|
|
|
|
raise YAML::TypeError, "Invalid Range: " + val.inspect
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( object_id, opts ) do |out|
|
|
|
|
# if self.begin.is_complex_yaml? or self.begin.respond_to? :to_str or
|
|
|
|
# self.end.is_complex_yaml? or self.end.respond_to? :to_str or
|
|
|
|
# not to_yaml_properties.empty?
|
|
|
|
out.map( taguri, to_yaml_style ) do |map|
|
2004-05-06 02:29:56 -04:00
|
|
|
map.add( 'begin', self.begin )
|
|
|
|
map.add( 'end', self.end )
|
|
|
|
map.add( 'excl', self.exclude_end? )
|
2005-09-12 23:58:33 -04:00
|
|
|
to_yaml_properties.each do |m|
|
2004-05-06 02:29:56 -04:00
|
|
|
map.add( m, instance_variable_get( m ) )
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
# else
|
|
|
|
# out.scalar( taguri ) do |sc|
|
|
|
|
# sc.embed( self.begin )
|
|
|
|
# sc.concat( self.exclude_end? ? "..." : ".." )
|
|
|
|
# sc.embed( self.end )
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Regexp
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:ruby.yaml.org,2002:regexp"
|
|
|
|
def Regexp.yaml_new( klass, tag, val )
|
|
|
|
if String === val and val =~ /^\/(.*)\/([mix]*)$/
|
|
|
|
val = { 'regexp' => $1, 'mods' => $2 }
|
|
|
|
end
|
|
|
|
if Hash === val
|
|
|
|
mods = nil
|
|
|
|
unless val['mods'].to_s.empty?
|
|
|
|
mods = 0x00
|
|
|
|
mods |= Regexp::EXTENDED if val['mods'].include?( 'x' )
|
|
|
|
mods |= Regexp::IGNORECASE if val['mods'].include?( 'i' )
|
|
|
|
mods |= Regexp::MULTILINE if val['mods'].include?( 'm' )
|
|
|
|
end
|
|
|
|
val.delete( 'mods' )
|
|
|
|
r = YAML::object_maker( klass, {} )
|
|
|
|
Regexp.instance_method(:initialize).
|
|
|
|
bind(r).
|
|
|
|
call( val.delete( 'regexp' ), mods )
|
|
|
|
val.each { |k,v| r.instance_variable_set( k, v ) }
|
|
|
|
r
|
|
|
|
else
|
|
|
|
raise YAML::TypeError, "Invalid Regular expression: " + val.inspect
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( nil, opts ) do |out|
|
|
|
|
if to_yaml_properties.empty?
|
|
|
|
out.scalar( taguri, self.inspect, :plain )
|
|
|
|
else
|
|
|
|
out.map( taguri, to_yaml_style ) do |map|
|
2004-05-06 02:29:56 -04:00
|
|
|
src = self.inspect
|
|
|
|
if src =~ /\A\/(.*)\/([a-z]*)\Z/
|
|
|
|
map.add( 'regexp', $1 )
|
|
|
|
map.add( 'mods', $2 )
|
|
|
|
else
|
2005-09-12 23:58:33 -04:00
|
|
|
raise YAML::TypeError, "Invalid Regular expression: " + src
|
2004-05-06 02:29:56 -04:00
|
|
|
end
|
2005-09-12 23:58:33 -04:00
|
|
|
to_yaml_properties.each do |m|
|
2004-05-06 02:29:56 -04:00
|
|
|
map.add( m, instance_variable_get( m ) )
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
|
|
|
end
|
2004-05-06 02:29:56 -04:00
|
|
|
end
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Time
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:ruby.yaml.org,2002:time"
|
|
|
|
yaml_as "tag:yaml.org,2002:timestamp"
|
|
|
|
def Time.yaml_new( klass, tag, val )
|
|
|
|
if Hash === val
|
|
|
|
t = val.delete( 'at' )
|
|
|
|
val.each { |k,v| t.instance_variable_set( k, v ) }
|
|
|
|
t
|
|
|
|
else
|
|
|
|
raise YAML::TypeError, "Invalid Time: " + val.inspect
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( object_id, opts ) do |out|
|
|
|
|
tz = "Z"
|
|
|
|
# from the tidy Tobias Peters <t-peters@gmx.de> Thanks!
|
|
|
|
unless self.utc?
|
|
|
|
utc_same_instant = self.dup.utc
|
|
|
|
utc_same_writing = Time.utc(year,month,day,hour,min,sec,usec)
|
|
|
|
difference_to_utc = utc_same_writing - utc_same_instant
|
|
|
|
if (difference_to_utc < 0)
|
|
|
|
difference_sign = '-'
|
|
|
|
absolute_difference = -difference_to_utc
|
|
|
|
else
|
|
|
|
difference_sign = '+'
|
|
|
|
absolute_difference = difference_to_utc
|
|
|
|
end
|
|
|
|
difference_minutes = (absolute_difference/60).round
|
|
|
|
tz = "%s%02d:%02d" % [ difference_sign, difference_minutes / 60, difference_minutes % 60]
|
|
|
|
end
|
|
|
|
standard = self.strftime( "%Y-%m-%d %H:%M:%S" )
|
|
|
|
standard += ".%06d" % [usec] if usec.nonzero?
|
|
|
|
standard += " %s" % [tz]
|
|
|
|
if to_yaml_properties.empty?
|
|
|
|
out.scalar( taguri, standard, :plain )
|
2004-05-06 02:29:56 -04:00
|
|
|
else
|
2005-09-12 23:58:33 -04:00
|
|
|
out.map( taguri, to_yaml_style ) do |map|
|
|
|
|
map.add( 'at', standard )
|
|
|
|
to_yaml_properties.each do |m|
|
|
|
|
map.add( m, instance_variable_get( m ) )
|
2004-05-06 02:29:56 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Date
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:yaml.org,2002:timestamp#ymd"
|
2003-05-09 17:25:50 -04:00
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( object_id, opts ) do |out|
|
|
|
|
out.scalar( "tag:yaml.org,2002:timestamp", self.to_s, :plain )
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Numeric
|
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( nil, opts ) do |out|
|
|
|
|
str = self.to_s
|
|
|
|
if str == "Infinity"
|
|
|
|
str = ".Inf"
|
|
|
|
elsif str == "-Infinity"
|
|
|
|
str = "-.Inf"
|
|
|
|
elsif str == "NaN"
|
|
|
|
str = ".NaN"
|
|
|
|
end
|
|
|
|
out.scalar( taguri, str, :plain )
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
2005-09-17 13:22:49 -04:00
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
class Fixnum
|
|
|
|
yaml_as "tag:yaml.org,2002:int"
|
|
|
|
end
|
2005-09-17 13:22:49 -04:00
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
class Float
|
|
|
|
yaml_as "tag:yaml.org,2002:float"
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
class TrueClass
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:yaml.org,2002:bool#yes"
|
2003-05-09 17:25:50 -04:00
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( nil, opts ) do |out|
|
|
|
|
out.scalar( taguri, "true", :plain )
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FalseClass
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:yaml.org,2002:bool#no"
|
2003-05-09 17:25:50 -04:00
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( nil, opts ) do |out|
|
|
|
|
out.scalar( taguri, "false", :plain )
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class NilClass
|
2005-09-12 23:58:33 -04:00
|
|
|
yaml_as "tag:yaml.org,2002:null"
|
2003-05-09 17:25:50 -04:00
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( nil, opts ) do |out|
|
|
|
|
out.scalar( taguri, "", :plain )
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|