diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2009-11-17 14:21:48 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2009-11-17 14:21:48 -0800 |
commit | bffb39d682675904158f43bfafebace0c50ed879 (patch) | |
tree | cc1f4d2fe3db4e84431eb6d8c16e907fb7771f23 /lib | |
parent | 59f905bc00da88a6cb4d0edf73aa5b8201124bd1 (diff) | |
download | psych-bffb39d682675904158f43bfafebace0c50ed879.zip |
integrating some yaml tests
Diffstat (limited to 'lib')
-rw-r--r-- | lib/psych/ruby.rb | 2 | ||||
-rw-r--r-- | lib/psych/scalar_scanner.rb | 8 | ||||
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 50 | ||||
-rw-r--r-- | lib/psych/visitors/yast_builder.rb | 16 |
4 files changed, 53 insertions, 23 deletions
diff --git a/lib/psych/ruby.rb b/lib/psych/ruby.rb index 47e4421..3a2e124 100644 --- a/lib/psych/ruby.rb +++ b/lib/psych/ruby.rb @@ -2,6 +2,8 @@ require 'complex' require 'rational' require 'date' +YAML = Psych + [ Object, String, Class, Hash, Array, NilClass, Float, FalseClass, TrueClass, Range, Complex, Rational, Date, Time, Regexp, Exception, Struct diff --git a/lib/psych/scalar_scanner.rb b/lib/psych/scalar_scanner.rb index f6412eb..2a3afa9 100644 --- a/lib/psych/scalar_scanner.rb +++ b/lib/psych/scalar_scanner.rb @@ -36,8 +36,12 @@ module Psych [:NEGATIVE_INFINITY, -1 / 0.0] when /^\.nan$/i [:NAN, 0.0 / 0.0] - when /^:.+/i - [:SYMBOL, @string.sub(/^:/, '').to_sym] + when /^:.+/ + if @string =~ /^:(["'])(.*)\1/ + [:SYMBOL, $2.sub(/^:/, '').to_sym] + else + [:SYMBOL, @string.sub(/^:/, '').to_sym] + end when /^[-+]?[1-9][0-9_]*(:[0-5]?[0-9])+$/ i = 0 @string.split(':').each_with_index do |n,e| diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index f91537f..d4a48a1 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -84,26 +84,11 @@ module Psych end string when /!ruby\/struct:?(.*)?$/ - klassname = $1 + klass = resolve_class($1) members = o.children.map { |c| accept c } - if klassname && klassname.length > 1 - name = klassname - s = nil - retried = false - - begin - s = name.split('::').inject(Object) { |k,sub| - k.const_get sub - }.allocate - rescue NameError => ex - name = "Struct::#{name}" - unless retried - retried = true - retry - end - raise ex - end + if klass + s = klass.allocate struct_members = s.members.map { |x| x.to_sym } members.each_slice(2) { |k,v| if struct_members.include? k.to_sym @@ -122,9 +107,13 @@ module Psych h = Hash[*o.children.map { |c| accept c }] Range.new(h['begin'], h['end'], h['excl']) - when "!ruby/exception" + when /!ruby\/exception:?(.*)?$/ h = Hash[*o.children.map { |c| accept c }] - Exception.new h['message'] + + e = build_exception((resolve_class($1) || Exception), h.delete('message')) + + h.each { |k,v| e.instance_variable_set :"@#{k}", v } + e when '!ruby/object:Complex' h = Hash[*o.children.map { |c| accept c }] @@ -166,6 +155,27 @@ module Psych end private + # Convert +klassname+ to a Class + def resolve_class klassname + return nil unless klassname and not klassname.empty? + + name = klassname + retried = false + + begin + name.split('::').inject(Object) { |k,sub| + k.const_get sub + } + rescue NameError => ex + name = "Struct::#{name}" + unless retried + retried = true + retry + end + raise ex + end + end + def resolve_unknown o token = ScalarScanner.new(o.value).tokenize diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb index 6e2295c..4eb21fd 100644 --- a/lib/psych/visitors/yast_builder.rb +++ b/lib/psych/visitors/yast_builder.rb @@ -62,10 +62,24 @@ module Psych end def visit_Exception o - @stack.push append Nodes::Mapping.new(nil, '!ruby/exception', false) + tag = ['!ruby/exception', o.class.name].compact.join(':') + @stack.push append Nodes::Mapping.new(nil, tag, false) + ['message', o.message].each do |m| accept m end + + if o.respond_to? :to_yaml_properties + ivars = o.to_yaml_properties + else + ivars = o.instance_variables + end + + ivars.each do |iv| + accept iv.to_s.sub(/^@/, '') + accept o.instance_variable_get(iv) + end + @stack.pop end |