From 3915f5b7f9650e563db18d6869cc8ed2de1c3bba Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 21 Aug 2014 15:35:55 +0300 Subject: [PATCH] fix the missing reference bug on class --- lib/sof/members.rb | 2 +- lib/sof/node.rb | 15 ++++++++++----- lib/sof/occurence.rb | 8 ++++++-- lib/sof/writer.rb | 2 +- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/sof/members.rb b/lib/sof/members.rb index 8973a2b7..13e5c30f 100644 --- a/lib/sof/members.rb +++ b/lib/sof/members.rb @@ -21,7 +21,7 @@ module Sof end unless occurence.referenced #puts "referencing #{@counter} , at level #{level}/#{occurence.level} " - occurence.referenced = @counter + occurence.set_reference(@counter) @counter = @counter + 1 end return diff --git a/lib/sof/node.rb b/lib/sof/node.rb index b09463b6..713d0df9 100644 --- a/lib/sof/node.rb +++ b/lib/sof/node.rb @@ -4,13 +4,18 @@ module Sof #abstract base class for nodes in the tree # may be referenced (should be a simple name or number) class Node - def initialize referenced - @referenced = referenced - end include Util + def initialize ref + @referenced = ref + end # must be able to output to a stream def out io ,level - io.write "&#{referenced} " if referenced + io.write "&#{@referenced} " if @referenced + end + def as_string(level) + io = StringIO.new + out(io,level) + io.string end attr_reader :referenced end @@ -22,7 +27,7 @@ module Sof end attr_reader :data def out io , level - super + super(io,level) io.write(data) end end diff --git a/lib/sof/occurence.rb b/lib/sof/occurence.rb index bcc39a24..79b4f306 100644 --- a/lib/sof/occurence.rb +++ b/lib/sof/occurence.rb @@ -4,9 +4,13 @@ module Sof def initialize object , level @object = object @level = level + @referenced = nil end - attr_reader :object - attr_accessor :level , :referenced + def set_reference r + @referenced = r + end + attr_reader :object , :referenced + attr_accessor :level end end diff --git a/lib/sof/writer.rb b/lib/sof/writer.rb index 28131041..6b205071 100644 --- a/lib/sof/writer.rb +++ b/lib/sof/writer.rb @@ -42,7 +42,7 @@ module Sof atts[a] = to_sof_node(val , level + 1) end immediate , extended = atts.partition {|a,val| val.is_a?(SimpleNode) } - head += immediate.collect {|a,val| "#{a.to_sof()} => #{val.data}"}.join(", ") + ")" + head += immediate.collect {|a,val| "#{a.to_sof()} => #{val.as_string(level)}"}.join(", ") + ")" return SimpleNode.new(head) if( ref.nil? and extended.empty? and head.length < 30 ) node = ObjectNode.new(head , ref) extended.each do |a , val|