lib/psych/visitors/yaml_tree.rb in psych-2.0.5 vs lib/psych/visitors/yaml_tree.rb in psych-2.0.6

- old
+ new

@@ -14,14 +14,16 @@ class YAMLTree < Psych::Visitors::Visitor class Registrar # :nodoc: def initialize @obj_to_id = {} @obj_to_node = {} + @targets = [] @counter = 0 end def register target, node + @targets << target @obj_to_node[target.object_id] = node end def key? target @obj_to_node.key? target.object_id @@ -207,10 +209,29 @@ dump_ivars o @emitter.end_mapping end + def visit_NameError o + tag = ['!ruby/exception', o.class.name].join ':' + + @emitter.start_mapping nil, tag, false, Nodes::Mapping::BLOCK + + { + 'message' => o.message.to_s, + 'backtrace' => private_iv_get(o, 'backtrace'), + }.each do |k,v| + next unless v + @emitter.scalar k, nil, nil, true, false, Nodes::Scalar::ANY + accept v + end + + dump_ivars o + + @emitter.end_mapping + end + def visit_Regexp o register o, @emitter.scalar(o.inspect, nil, '!ruby/regexp', false, false, Nodes::Scalar::ANY) end def visit_DateTime o @@ -287,10 +308,15 @@ style = Nodes::Scalar::LITERAL plain = false quote = false elsif o =~ /\n/ style = Nodes::Scalar::LITERAL + elsif o == '<<' + style = Nodes::Scalar::SINGLE_QUOTED + tag = 'tag:yaml.org,2002:str' + plain = false + quote = false elsif o =~ /^\W[^"]*$/ style = Nodes::Scalar::DOUBLE_QUOTED else unless String === @ss.tokenize(o) style = Nodes::Scalar::SINGLE_QUOTED @@ -376,10 +402,14 @@ def visit_NilClass o @emitter.scalar('', nil, 'tag:yaml.org,2002:null', true, false, Nodes::Scalar::ANY) end def visit_Symbol o - @emitter.scalar ":#{o}", nil, nil, true, false, Nodes::Scalar::ANY + if o.empty? + @emitter.scalar "", nil, '!ruby/symbol', false, false, Nodes::Scalar::ANY + else + @emitter.scalar ":#{o}", nil, nil, true, false, Nodes::Scalar::ANY + end end private # FIXME: Remove the index and count checks in Psych 3.0 NULL = "\x00"