Skip to content

Commit

Permalink
* ext/psych/lib/psych/visitors/yaml_tree.rb: change output reference
Browse files Browse the repository at this point in the history
  ids to be sequential numbers.
  • Loading branch information
tenderlove committed Feb 7, 2013
1 parent 030cfe9 commit 4ed9ce4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Fri Feb 8 08:50:42 2013 Aaron Patterson <aaron@tenderlovemaking.com>

* ext/psych/lib/psych/visitors/yaml_tree.rb: change output reference
ids to be sequential numbers.

Thu Jan 17 10:48:56 2013 Aaron Patterson <aaron@tenderlovemaking.com>

* ext/psych/lib/psych/scalar_scanner.rb: use constants rather than
Expand Down
34 changes: 29 additions & 5 deletions lib/psych/visitors/yaml_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ module Visitors
# builder.tree # => #<Psych::Nodes::Stream .. }
#
class YAMLTree < Psych::Visitors::Visitor
class Registrar # :nodoc:
def initialize
@obj_to_id = {}
@obj_to_node = {}
@counter = 0
end

def register target, node
@obj_to_node[target.object_id] = node
end

def key? target
@obj_to_node.key? target.object_id
end

def id_for target
@obj_to_id[target.object_id] ||= (@counter += 1)
end

def node_for target
@obj_to_node[target.object_id]
end
end

attr_reader :started, :finished
alias :finished? :finished
alias :started? :started
Expand All @@ -17,7 +41,7 @@ def initialize options = {}, emitter = TreeBuilder.new, ss = ScalarScanner.new
@started = false
@finished = false
@emitter = emitter
@st = {}
@st = Registrar.new
@ss = ss
@options = options
@coders = []
Expand Down Expand Up @@ -72,9 +96,9 @@ def push object

def accept target
# return any aliases we find
if @st.key? target.object_id
oid = target.object_id
node = @st[oid]
if @st.key? target
oid = @st.id_for target
node = @st.node_for target
anchor = oid.to_s
node.anchor = anchor
return @emitter.alias anchor
Expand Down Expand Up @@ -410,7 +434,7 @@ def find_ivars target
end

def register target, yaml_obj
@st[target.object_id] = yaml_obj
@st.register target, yaml_obj
yaml_obj
end

Expand Down

0 comments on commit 4ed9ce4

Please sign in to comment.