diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2009-12-18 17:40:46 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2009-12-18 17:40:46 -0800 |
commit | d220465e179f430a72be71c649957f0582226b15 (patch) | |
tree | 19c1b8ed0dae96d4987f060ecb58e213848b9dcd /lib | |
parent | 348d95c4a4e5bf59ad6b7a1f0952d94ade0f5f63 (diff) | |
download | psych-d220465e179f430a72be71c649957f0582226b15.zip |
refactoring ivar dumping
Diffstat (limited to 'lib')
-rw-r--r-- | lib/psych/visitors/yaml_tree.rb | 59 |
1 files changed, 24 insertions, 35 deletions
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb index 5562f14..ceea9dc 100644 --- a/lib/psych/visitors/yaml_tree.rb +++ b/lib/psych/visitors/yaml_tree.rb @@ -43,19 +43,10 @@ module Psych klass = o.class == Object ? nil : o.class.name tag = ['!ruby/object', klass].compact.join(':') - mapping = Nodes::Mapping.new(nil, tag, false) - @stack.push append mapping + map = append Nodes::Mapping.new(nil, tag, false) - if o.respond_to? :to_yaml_properties - ivars = o.to_yaml_properties - else - ivars = o.instance_variables - end - - ivars.each do |iv| - mapping.children << Nodes::Scalar.new(":#{iv.to_s.sub(/^@/, '')}") - accept o.instance_variable_get(iv) - end + @stack.push map + dump_ivars(o, map) @stack.pop end @@ -65,49 +56,34 @@ module Psych map = register(o, Nodes::Mapping.new(nil, tag, false)) @stack.push append map + o.members.each do |member| - accept member + map.children << Nodes::Scalar.new(":#{member}") accept o[member] end - if o.respond_to? :to_yaml_properties - ivars = o.to_yaml_properties - else - ivars = o.instance_variables - end + dump_ivars(o, map) - ivars.each do |iv| - map.children << Nodes::Scalar.new(":#{iv.to_s.sub(/^@/, '')}") - accept o.instance_variable_get(iv) - end @stack.pop end def visit_Exception o tag = ['!ruby/exception', o.class.name].join ':' - mapping = Nodes::Mapping.new(nil, tag, false) - @stack.push append mapping + map = append Nodes::Mapping.new(nil, tag, false) + + @stack.push map { 'message' => private_iv_get(o, 'mesg'), 'backtrace' => private_iv_get(o, 'backtrace'), }.each do |k,v| next unless v - append Nodes::Scalar.new k + map.children << Nodes::Scalar.new(k) accept v end - if o.respond_to? :to_yaml_properties - ivars = o.to_yaml_properties - else - ivars = o.instance_variables - end - - ivars.each do |iv| - mapping.children << Nodes::Scalar.new(":#{iv.to_s.sub(/^@/, '')}") - accept o.instance_variable_get(iv) - end + dump_ivars(o, map) @stack.pop end @@ -260,6 +236,19 @@ module Psych @st[target.object_id] = yaml_obj yaml_obj end + + def dump_ivars target, map + if target.respond_to? :to_yaml_properties + ivars = target.to_yaml_properties + else + ivars = target.instance_variables + end + + ivars.each do |iv| + map.children << Nodes::Scalar.new(":#{iv.to_s.sub(/^@/, '')}") + accept target.instance_variable_get(iv) + end + end end end end |