summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorwhy <why@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-05-22 17:56:30 +0000
committerwhy <why@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-05-22 17:56:30 +0000
commit9bb41601890f6f224bdb6370476b9295a19598fc (patch)
tree0c3e7da63d97e8b40f2335da403170d5a2b7ad5d /lib
parent3458bf43288ae4b76564e538f35e9c423ea4f620 (diff)
* lib/token.c: single- and double-quoted root-level fix.
* lib/yaml.rb (YAML::object_maker): can create object attributes (such as found in Exception class) * lib/yaml/rubytypes.rb: roundtripping of Exception and subclasses. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3856 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/yaml.rb17
-rw-r--r--lib/yaml/rubytypes.rb35
2 files changed, 45 insertions, 7 deletions
diff --git a/lib/yaml.rb b/lib/yaml.rb
index a8bba691f0..161979bb52 100644
--- a/lib/yaml.rb
+++ b/lib/yaml.rb
@@ -124,13 +124,20 @@ module YAML
#
# Allocate blank object
#
- def YAML.object_maker( obj_class, val )
+ def YAML.object_maker( obj_class, val, is_attr = false )
if Hash === val
name = obj_class.name
- o = ::Marshal.load( sprintf( "\004\006o:%c%s\000", name.length + 5, name ))
- val.each_pair { |k,v|
- o.instance_eval "@#{k} = v"
- }
+ ostr = sprintf( "\004\006o:%c%s\000", name.length + 5, name )
+ if is_attr
+ ostr[ -1, 1 ] = Marshal.dump( val ).sub( /^[^{]+\{/, '' )
+ p ostr
+ end
+ o = ::Marshal.load( ostr )
+ unless is_attr
+ val.each_pair { |k,v|
+ o.instance_eval "@#{k} = v"
+ }
+ end
o
else
raise YAML::Error, "Invalid object explicitly tagged !ruby/Object: " + val.inspect
diff --git a/lib/yaml/rubytypes.rb b/lib/yaml/rubytypes.rb
index a670237635..92aee82dae 100644
--- a/lib/yaml/rubytypes.rb
+++ b/lib/yaml/rubytypes.rb
@@ -1,5 +1,4 @@
require 'date'
-require 'yaml/constants'
#
# Type conversions
#
@@ -242,6 +241,38 @@ YAML.add_builtin_type( 'seq', &array_proc )
YAML.add_ruby_type( 'array', &array_proc )
#
+# 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|
+ map.add( m[1..-1], instance_eval( m ) )
+ }
+ }
+ }
+ end
+end
+
+YAML.add_ruby_type( 'exception' ) { |type, val|
+ 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|
+ o.instance_eval "@#{k} = v"
+ }
+ o
+}
+
+
+#
# String#to_yaml
#
class String
@@ -274,7 +305,7 @@ class String
"''"
elsif YAML.detect_implicit( self ) != 'str'
"\"#{YAML.escape( self )}\""
- elsif self =~ /#{YAML::ESCAPE_CHAR}|[#{YAML::SPACE_INDICATORS}]( |\n|$)|\'/
+ elsif self =~ /#{YAML::ESCAPE_CHAR}|[#{YAML::SPACE_INDICATORS}] |\n|\'/
"\"#{YAML.escape( self )}\""
elsif self =~ /^[^#{YAML::WORD_CHAR}]/
"\"#{YAML.escape( self )}\""