2003-12-20 05:40:15 +03:00
|
|
|
# -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- vim: sw=4 ts=4
|
2003-05-10 01:25:50 +04:00
|
|
|
require 'date'
|
|
|
|
#
|
|
|
|
# Type conversions
|
|
|
|
#
|
2003-05-14 02:29:52 +04:00
|
|
|
|
2003-05-22 09:41:06 +04:00
|
|
|
class Class
|
|
|
|
def to_yaml( opts = {} )
|
2004-05-06 10:29:56 +04:00
|
|
|
raise TypeError, "can't dump anonymous class %s" % self.class
|
2003-05-22 09:41:06 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-05-10 01:25:50 +04:00
|
|
|
class Object
|
|
|
|
def is_complex_yaml?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def to_yaml_type
|
|
|
|
"!ruby/object:#{self.class}"
|
|
|
|
end
|
|
|
|
def to_yaml_properties
|
|
|
|
instance_variables.sort
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2003-05-14 02:29:52 +04:00
|
|
|
YAML::quick_emit( self.object_id, opts ) { |out|
|
2003-05-10 01:25:50 +04:00
|
|
|
out.map( self.to_yaml_type ) { |map|
|
|
|
|
to_yaml_properties.each { |m|
|
2003-12-20 05:40:15 +03:00
|
|
|
map.add( m[1..-1], instance_variable_get( m ) )
|
2003-05-10 01:25:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-06-18 05:06:00 +04:00
|
|
|
YAML.add_ruby_type( /^object/ ) { |type, val|
|
2003-05-10 01:25:50 +04:00
|
|
|
type, obj_class = YAML.read_type_class( type, Object )
|
|
|
|
YAML.object_maker( obj_class, val )
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Maps: Hash#to_yaml
|
|
|
|
#
|
|
|
|
class Hash
|
|
|
|
def is_complex_yaml?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def to_yaml_type
|
|
|
|
if self.class == Hash or self.class == YAML::SpecialHash
|
|
|
|
"!map"
|
|
|
|
else
|
|
|
|
"!ruby/hash:#{self.class}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
|
|
|
opts[:DocType] = self.class if Hash === opts
|
2003-05-14 02:29:52 +04:00
|
|
|
YAML::quick_emit( self.object_id, opts ) { |out|
|
2003-05-10 01:25:50 +04:00
|
|
|
hash_type = to_yaml_type
|
2003-07-12 02:52:14 +04:00
|
|
|
if not out.options(:ExplicitTypes) and hash_type == "!map"
|
2003-05-10 01:25:50 +04:00
|
|
|
hash_type = ""
|
|
|
|
end
|
|
|
|
out.map( hash_type ) { |map|
|
|
|
|
#
|
|
|
|
# Sort the hash
|
|
|
|
#
|
2003-07-12 02:52:14 +04:00
|
|
|
if out.options(:SortKeys)
|
2003-05-10 01:25:50 +04:00
|
|
|
map.concat( self.sort )
|
|
|
|
else
|
|
|
|
map.concat( self.to_a )
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
hash_proc = Proc.new { |type, val|
|
|
|
|
if Array === val
|
|
|
|
val = Hash.[]( *val ) # Convert the map to a sequence
|
|
|
|
elsif Hash === val
|
|
|
|
type, obj_class = YAML.read_type_class( type, Hash )
|
|
|
|
if obj_class != Hash
|
|
|
|
o = obj_class.new
|
|
|
|
o.update( val )
|
|
|
|
val = o
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise YAML::Error, "Invalid map explicitly tagged !map: " + val.inspect
|
|
|
|
end
|
|
|
|
val
|
|
|
|
}
|
2003-06-18 05:06:00 +04:00
|
|
|
YAML.add_ruby_type( /^hash/, &hash_proc )
|
2003-05-10 01:25:50 +04:00
|
|
|
|
|
|
|
module YAML
|
|
|
|
|
|
|
|
#
|
|
|
|
# Ruby-specific collection: !ruby/flexhash
|
|
|
|
#
|
|
|
|
class FlexHash < Array
|
|
|
|
def []( k )
|
|
|
|
self.assoc( k ).to_a[1]
|
|
|
|
end
|
|
|
|
def []=( k, *rest )
|
|
|
|
val, set = rest.reverse
|
|
|
|
if ( tmp = self.assoc( k ) ) and not set
|
|
|
|
tmp[1] = val
|
|
|
|
else
|
|
|
|
self << [ k, val ]
|
|
|
|
end
|
|
|
|
val
|
|
|
|
end
|
|
|
|
def has_key?( k )
|
|
|
|
self.assoc( k ) ? true : false
|
|
|
|
end
|
|
|
|
def is_complex_yaml?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2003-05-14 02:29:52 +04:00
|
|
|
YAML::quick_emit( self.object_id, opts ) { |out|
|
2003-05-10 01:25:50 +04:00
|
|
|
out.seq( "!ruby/flexhash" ) { |seq|
|
|
|
|
self.each { |v|
|
|
|
|
if v[1]
|
|
|
|
seq.add( Hash.[]( *v ) )
|
|
|
|
else
|
|
|
|
seq.add( v[0] )
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-05-11 10:32:13 +04:00
|
|
|
YAML.add_ruby_type( 'flexhash' ) { |type, val|
|
2003-05-10 01:25:50 +04:00
|
|
|
if Array === val
|
|
|
|
p = FlexHash.new
|
|
|
|
val.each { |v|
|
|
|
|
if Hash === v
|
|
|
|
p.concat( v.to_a ) # Convert the map to a sequence
|
|
|
|
else
|
|
|
|
p << [ v, nil ]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
p
|
|
|
|
else
|
|
|
|
raise YAML::Error, "Invalid !ruby/flexhash: " + val.inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Structs: export as a !map
|
|
|
|
#
|
|
|
|
class Struct
|
|
|
|
def is_complex_yaml?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2003-05-14 02:29:52 +04:00
|
|
|
YAML::quick_emit( self.object_id, opts ) { |out|
|
2003-05-10 01:25:50 +04:00
|
|
|
#
|
|
|
|
# Basic struct is passed as a YAML map
|
|
|
|
#
|
2003-12-12 21:08:58 +03:00
|
|
|
struct_name = self.class.name.gsub( "Struct::", "" )
|
|
|
|
out.map( "!ruby/struct:#{struct_name}" ) { |map|
|
2003-05-10 01:25:50 +04:00
|
|
|
self.members.each { |m|
|
|
|
|
map.add( m, self[m] )
|
|
|
|
}
|
2004-05-06 10:29:56 +04:00
|
|
|
self.to_yaml_properties.each { |m|
|
|
|
|
map.add( m, instance_variable_get( m ) )
|
|
|
|
}
|
2003-05-10 01:25:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-06-18 05:06:00 +04:00
|
|
|
YAML.add_ruby_type( /^struct/ ) { |type, val|
|
2003-05-10 01:25:50 +04:00
|
|
|
if Hash === val
|
2003-05-13 10:34:18 +04:00
|
|
|
struct_type = nil
|
2003-05-10 01:25:50 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Use existing Struct if it exists
|
|
|
|
#
|
2004-05-06 10:29:56 +04:00
|
|
|
props = {}
|
|
|
|
val.delete_if { |k,v| props[k] = v if k =~ /^@/ }
|
2003-05-10 01:25:50 +04:00
|
|
|
begin
|
2003-05-13 10:34:18 +04:00
|
|
|
struct_name, struct_type = YAML.read_type_class( type, Struct )
|
2003-05-10 01:25:50 +04:00
|
|
|
rescue NameError
|
|
|
|
end
|
|
|
|
if not struct_type
|
2003-05-13 10:34:18 +04:00
|
|
|
struct_def = [ type.split( ':', 4 ).last ]
|
2003-05-10 01:25:50 +04:00
|
|
|
struct_type = Struct.new( *struct_def.concat( val.keys.collect { |k| k.intern } ) )
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Set the Struct properties
|
|
|
|
#
|
2004-05-06 10:29:56 +04:00
|
|
|
st = YAML::object_maker( struct_type, {} )
|
2003-05-10 01:25:50 +04:00
|
|
|
st.members.each { |m|
|
|
|
|
st.send( "#{m}=", val[m] )
|
|
|
|
}
|
2004-05-06 10:29:56 +04:00
|
|
|
props.each { |k,v|
|
|
|
|
st.instance_variable_set(k, v)
|
|
|
|
}
|
2003-05-10 01:25:50 +04:00
|
|
|
st
|
|
|
|
else
|
|
|
|
raise YAML::Error, "Invalid Ruby Struct: " + val.inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Sequences: Array#to_yaml
|
|
|
|
#
|
|
|
|
class Array
|
|
|
|
def is_complex_yaml?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def to_yaml_type
|
|
|
|
if self.class == Array
|
|
|
|
"!seq"
|
|
|
|
else
|
|
|
|
"!ruby/array:#{self.class}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
|
|
|
opts[:DocType] = self.class if Hash === opts
|
2003-05-14 02:29:52 +04:00
|
|
|
YAML::quick_emit( self.object_id, opts ) { |out|
|
2003-05-10 01:25:50 +04:00
|
|
|
array_type = to_yaml_type
|
2003-07-12 02:52:14 +04:00
|
|
|
if not out.options(:ExplicitTypes) and array_type == "!seq"
|
2003-05-10 01:25:50 +04:00
|
|
|
array_type = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
out.seq( array_type ) { |seq|
|
|
|
|
seq.concat( self )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
array_proc = Proc.new { |type, val|
|
|
|
|
if Array === val
|
|
|
|
type, obj_class = YAML.read_type_class( type, Array )
|
|
|
|
if obj_class != Array
|
|
|
|
o = obj_class.new
|
|
|
|
o.concat( val )
|
|
|
|
val = o
|
|
|
|
end
|
|
|
|
val
|
|
|
|
else
|
|
|
|
val.to_a
|
|
|
|
end
|
|
|
|
}
|
2003-06-18 05:06:00 +04:00
|
|
|
YAML.add_ruby_type( /^array/, &array_proc )
|
2003-05-10 01:25:50 +04:00
|
|
|
|
2003-05-22 21:56:30 +04:00
|
|
|
#
|
|
|
|
# Exception#to_yaml
|
|
|
|
#
|
|
|
|
class Exception
|
|
|
|
def is_complex_yaml?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def to_yaml_type
|
|
|
|
"!ruby/exception:#{self.class}"
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
|
|
|
YAML::quick_emit( self.object_id, opts ) { |out|
|
|
|
|
out.map( self.to_yaml_type ) { |map|
|
|
|
|
map.add( 'message', self.message )
|
|
|
|
to_yaml_properties.each { |m|
|
2003-12-20 05:40:15 +03:00
|
|
|
map.add( m[1..-1], instance_variable_get( m ) )
|
2003-05-22 21:56:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-06-18 05:06:00 +04:00
|
|
|
YAML.add_ruby_type( /^exception/ ) { |type, val|
|
2003-05-22 21:56:30 +04:00
|
|
|
type, obj_class = YAML.read_type_class( type, Exception )
|
|
|
|
o = YAML.object_maker( obj_class, { 'mesg' => val.delete( 'message' ) }, true )
|
|
|
|
val.each_pair { |k,v|
|
2003-12-20 05:40:15 +03:00
|
|
|
o.instance_variable_set("@#{k}", v)
|
2003-05-22 21:56:30 +04:00
|
|
|
}
|
|
|
|
o
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-10 01:25:50 +04:00
|
|
|
#
|
|
|
|
# String#to_yaml
|
|
|
|
#
|
|
|
|
class String
|
|
|
|
def is_complex_yaml?
|
2004-05-06 10:29:56 +04:00
|
|
|
to_yaml_fold or not to_yaml_properties.empty? or self =~ /\n.+/
|
2003-05-10 01:25:50 +04:00
|
|
|
end
|
|
|
|
def is_binary_data?
|
|
|
|
( self.count( "^ -~", "^\r\n" ) / self.size > 0.3 || self.count( "\x00" ) > 0 )
|
2004-05-06 10:29:56 +04:00
|
|
|
end
|
|
|
|
def to_yaml_type
|
2004-05-15 07:11:28 +04:00
|
|
|
"!ruby/string#{ ":#{ self.class }" if self.class != ::String }"
|
2004-05-06 10:29:56 +04:00
|
|
|
end
|
|
|
|
def to_yaml_fold
|
|
|
|
nil
|
2003-05-10 01:25:50 +04:00
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
|
|
|
complex = false
|
|
|
|
if self.is_complex_yaml?
|
|
|
|
complex = true
|
|
|
|
elsif opts[:BestWidth].to_i > 0
|
|
|
|
if self.length > opts[:BestWidth] and opts[:UseFold]
|
|
|
|
complex = true
|
|
|
|
end
|
|
|
|
end
|
2003-05-14 02:29:52 +04:00
|
|
|
YAML::quick_emit( complex ? self.object_id : nil, opts ) { |out|
|
2003-05-10 01:25:50 +04:00
|
|
|
if complex
|
2004-05-06 10:29:56 +04:00
|
|
|
if not to_yaml_properties.empty?
|
|
|
|
out.map( self.to_yaml_type ) { |map|
|
|
|
|
map.add( 'str', "#{self}" )
|
|
|
|
to_yaml_properties.each { |m|
|
|
|
|
map.add( m, instance_variable_get( m ) )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif self.is_binary_data?
|
2003-05-10 01:25:50 +04:00
|
|
|
out.binary_base64( self )
|
2004-05-15 07:11:28 +04:00
|
|
|
# elsif self =~ /^ |#{YAML::ESCAPE_CHAR}| $/
|
|
|
|
# complex = false
|
2003-05-10 01:25:50 +04:00
|
|
|
else
|
2004-05-06 10:29:56 +04:00
|
|
|
out.node_text( self, to_yaml_fold )
|
2003-05-10 01:25:50 +04:00
|
|
|
end
|
2003-06-10 18:15:27 +04:00
|
|
|
end
|
|
|
|
if not complex
|
2003-07-12 02:52:14 +04:00
|
|
|
ostr = if out.options(:KeepValue)
|
2003-05-10 01:25:50 +04:00
|
|
|
self
|
|
|
|
elsif empty?
|
|
|
|
"''"
|
2004-05-15 07:11:28 +04:00
|
|
|
elsif self =~ /^[^#{YAML::WORD_CHAR}\/]| \#|#{YAML::ESCAPE_CHAR}|[#{YAML::SPACE_INDICATORS}]( |$)| $|\n|\'/
|
2003-05-10 01:25:50 +04:00
|
|
|
"\"#{YAML.escape( self )}\""
|
2003-06-10 18:15:27 +04:00
|
|
|
elsif YAML.detect_implicit( self ) != 'str'
|
2003-05-10 01:25:50 +04:00
|
|
|
"\"#{YAML.escape( self )}\""
|
|
|
|
else
|
|
|
|
self
|
|
|
|
end
|
|
|
|
out.simple( ostr )
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-05-06 10:29:56 +04:00
|
|
|
YAML.add_ruby_type( /^string/ ) { |type, val|
|
|
|
|
type, obj_class = YAML.read_type_class( type, ::String )
|
|
|
|
if Hash === val
|
|
|
|
s = YAML::object_maker( obj_class, {} )
|
|
|
|
# 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::Error, "Invalid String: " + val.inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2003-05-10 01:25:50 +04:00
|
|
|
#
|
|
|
|
# Symbol#to_yaml
|
|
|
|
#
|
|
|
|
class Symbol
|
|
|
|
def is_complex_yaml?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
|
|
|
YAML::quick_emit( nil, opts ) { |out|
|
2004-04-03 03:31:14 +04:00
|
|
|
out << ":"
|
2003-05-22 09:41:06 +04:00
|
|
|
self.id2name.to_yaml( :Emitter => out )
|
2003-05-10 01:25:50 +04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
symbol_proc = Proc.new { |type, val|
|
|
|
|
if String === val
|
2004-05-15 08:38:39 +04:00
|
|
|
val = YAML::load( "--- #{val}") if val =~ /^["'].*['"]$/
|
2003-05-10 01:25:50 +04:00
|
|
|
val.intern
|
|
|
|
else
|
|
|
|
raise YAML::Error, "Invalid Symbol: " + val.inspect
|
|
|
|
end
|
|
|
|
}
|
2003-05-11 10:32:13 +04:00
|
|
|
YAML.add_ruby_type( 'symbol', &symbol_proc )
|
|
|
|
YAML.add_ruby_type( 'sym', &symbol_proc )
|
2003-05-10 01:25:50 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Range#to_yaml
|
|
|
|
#
|
|
|
|
class Range
|
|
|
|
def is_complex_yaml?
|
2004-05-06 10:29:56 +04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
def to_yaml_type
|
|
|
|
"!ruby/range#{ if self.class != ::Range; ":#{ self.class }"; end }"
|
2003-05-10 01:25:50 +04:00
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2004-05-06 10:29:56 +04:00
|
|
|
YAML::quick_emit( self.object_id, opts ) { |out|
|
2004-05-25 18:57:25 +04:00
|
|
|
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?
|
2004-05-06 10:29:56 +04:00
|
|
|
out.map( to_yaml_type ) { |map|
|
|
|
|
map.add( 'begin', self.begin )
|
|
|
|
map.add( 'end', self.end )
|
|
|
|
map.add( 'excl', self.exclude_end? )
|
|
|
|
to_yaml_properties.each { |m|
|
|
|
|
map.add( m, instance_variable_get( m ) )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
out << "#{ to_yaml_type } '"
|
|
|
|
self.begin.to_yaml(:Emitter => out)
|
|
|
|
out << ( self.exclude_end? ? "..." : ".." )
|
|
|
|
self.end.to_yaml(:Emitter => out)
|
|
|
|
out << "'"
|
|
|
|
end
|
2003-05-10 01:25:50 +04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-05-06 10:29:56 +04:00
|
|
|
YAML.add_ruby_type( /^range/ ) { |type, val|
|
|
|
|
type, obj_class = YAML.read_type_class( type, ::Range )
|
2004-05-15 08:38:39 +04:00
|
|
|
inr = %r'(\w+|[+-]?\d+(?:\.\d+)?(?:e[+-]\d+)?|"(?:[^\\"]|\\.)*")'
|
2004-05-06 10:29:56 +04:00
|
|
|
opts = {}
|
2004-05-15 08:38:39 +04:00
|
|
|
if String === val and val =~ /^#{inr}(\.{2,3})#{inr}$/o
|
2003-05-10 01:25:50 +04:00
|
|
|
r1, rdots, r2 = $1, $2, $3
|
2004-05-06 10:29:56 +04:00
|
|
|
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( obj_class, {} )
|
|
|
|
# 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
|
2003-05-10 01:25:50 +04:00
|
|
|
else
|
|
|
|
raise YAML::Error, "Invalid Range: " + val.inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2004-05-06 10:29:56 +04:00
|
|
|
# Make an Regexp
|
2003-05-10 01:25:50 +04:00
|
|
|
#
|
|
|
|
class Regexp
|
|
|
|
def is_complex_yaml?
|
2004-05-06 10:29:56 +04:00
|
|
|
self.class != Regexp or not to_yaml_properties.empty?
|
|
|
|
end
|
|
|
|
def to_yaml_type
|
|
|
|
"!ruby/regexp#{ if self.class != ::Regexp; ":#{ self.class }"; end }"
|
2003-05-10 01:25:50 +04:00
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
|
|
|
YAML::quick_emit( nil, opts ) { |out|
|
2004-05-06 10:29:56 +04:00
|
|
|
if self.is_complex_yaml?
|
|
|
|
out.map( self.to_yaml_type ) { |map|
|
|
|
|
src = self.inspect
|
|
|
|
if src =~ /\A\/(.*)\/([a-z]*)\Z/
|
|
|
|
map.add( 'regexp', $1 )
|
|
|
|
map.add( 'mods', $2 )
|
|
|
|
else
|
|
|
|
raise YAML::Error, "Invalid Regular expression: " + src
|
|
|
|
end
|
|
|
|
to_yaml_properties.each { |m|
|
|
|
|
map.add( m, instance_variable_get( m ) )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
out << "#{ to_yaml_type } "
|
|
|
|
self.inspect.to_yaml( :Emitter => out )
|
|
|
|
end
|
2003-05-10 01:25:50 +04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
regexp_proc = Proc.new { |type, val|
|
2004-05-06 10:29:56 +04:00
|
|
|
type, obj_class = YAML.read_type_class( type, ::Regexp )
|
2003-05-10 01:25:50 +04:00
|
|
|
if String === val and val =~ /^\/(.*)\/([mix]*)$/
|
2004-05-06 10:29:56 +04:00
|
|
|
val = { 'regexp' => $1, 'mods' => $2 }
|
2003-05-10 01:25:50 +04:00
|
|
|
end
|
|
|
|
if Hash === val
|
|
|
|
mods = nil
|
2004-05-06 10:29:56 +04:00
|
|
|
unless val['mods'].to_s.empty?
|
2003-05-10 01:25:50 +04:00
|
|
|
mods = 0x00
|
2004-05-06 10:29:56 +04:00
|
|
|
mods |= Regexp::EXTENDED if val['mods'].include?( 'x' )
|
|
|
|
mods |= Regexp::IGNORECASE if val['mods'].include?( 'i' )
|
|
|
|
mods |= Regexp::MULTILINE if val['mods'].include?( 'm' )
|
2003-05-10 01:25:50 +04:00
|
|
|
end
|
2004-05-06 10:29:56 +04:00
|
|
|
val.delete( 'mods' )
|
|
|
|
r = YAML::object_maker( obj_class, {} )
|
|
|
|
Regexp.instance_method(:initialize).
|
|
|
|
bind(r).
|
|
|
|
call( val.delete( 'regexp' ), mods )
|
|
|
|
val.each { |k,v| r.instance_variable_set( k, v ) }
|
|
|
|
r
|
2003-05-10 01:25:50 +04:00
|
|
|
else
|
|
|
|
raise YAML::Error, "Invalid Regular expression: " + val.inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
YAML.add_domain_type( "perl.yaml.org,2002", /^regexp/, ®exp_proc )
|
2004-05-06 10:29:56 +04:00
|
|
|
YAML.add_ruby_type( /^regexp/, ®exp_proc )
|
2003-05-10 01:25:50 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Emit a Time object as an ISO 8601 timestamp
|
|
|
|
#
|
|
|
|
class Time
|
|
|
|
def is_complex_yaml?
|
2004-05-06 10:29:56 +04:00
|
|
|
self.class != Time or not to_yaml_properties.empty?
|
|
|
|
end
|
|
|
|
def to_yaml_type
|
|
|
|
"!ruby/time#{ if self.class != ::Time; ":#{ self.class }"; end }"
|
2003-05-10 01:25:50 +04:00
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
|
|
|
YAML::quick_emit( nil, opts ) { |out|
|
2004-05-06 10:29:56 +04:00
|
|
|
if self.is_complex_yaml?
|
|
|
|
out.map( self.to_yaml_type ) { |map|
|
|
|
|
map.add( 'at', ::Time.at( self ) )
|
|
|
|
to_yaml_properties.each { |m|
|
|
|
|
map.add( m, instance_variable_get( m ) )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
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]
|
2003-05-10 01:25:50 +04:00
|
|
|
end
|
2004-05-06 10:29:56 +04:00
|
|
|
standard = self.strftime( "%Y-%m-%d %H:%M:%S" )
|
|
|
|
standard += ".%06d" % [usec] if usec.nonzero?
|
|
|
|
standard += " %s" % [tz]
|
|
|
|
standard.to_yaml( :Emitter => out, :KeepValue => true )
|
2003-05-10 01:25:50 +04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-05-06 10:29:56 +04:00
|
|
|
YAML.add_ruby_type( /^time/ ) { |type, val|
|
|
|
|
type, obj_class = YAML.read_type_class( type, ::Time )
|
|
|
|
if Hash === val
|
|
|
|
t = obj_class.at( val.delete( 'at' ) )
|
|
|
|
val.each { |k,v| t.instance_variable_set( k, v ) }
|
|
|
|
t
|
|
|
|
else
|
|
|
|
raise YAML::Error, "Invalid Time: " + val.inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2003-05-10 01:25:50 +04:00
|
|
|
#
|
|
|
|
# Emit a Date object as a simple implicit
|
|
|
|
#
|
|
|
|
class Date
|
|
|
|
def is_complex_yaml?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
|
|
|
opts[:KeepValue] = true
|
|
|
|
self.to_s.to_yaml( opts )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Send Integer, Booleans, NilClass to String
|
|
|
|
#
|
|
|
|
class Numeric
|
|
|
|
def is_complex_yaml?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
|
|
|
str = self.to_s
|
|
|
|
if str == "Infinity"
|
|
|
|
str = ".Inf"
|
|
|
|
elsif str == "-Infinity"
|
|
|
|
str = "-.Inf"
|
|
|
|
elsif str == "NaN"
|
|
|
|
str = ".NaN"
|
|
|
|
end
|
|
|
|
opts[:KeepValue] = true
|
|
|
|
str.to_yaml( opts )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TrueClass
|
|
|
|
def is_complex_yaml?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
|
|
|
opts[:KeepValue] = true
|
|
|
|
"true".to_yaml( opts )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FalseClass
|
|
|
|
def is_complex_yaml?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
|
|
|
opts[:KeepValue] = true
|
|
|
|
"false".to_yaml( opts )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class NilClass
|
|
|
|
def is_complex_yaml?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
|
|
|
opts[:KeepValue] = true
|
|
|
|
"".to_yaml( opts )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|