From ad4aaaff8a8eb4068c412a119515ee5949edc49a Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 18 Aug 2014 13:22:03 +0300 Subject: [PATCH] fix recursive arrays --- lib/sof/array.rb | 2 +- lib/sof/hash.rb | 4 ++-- lib/sof/writer.rb | 11 +++++++---- test/sof.rb | 6 ++++++ 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/sof/array.rb b/lib/sof/array.rb index 17d7fbe7..a91878cc 100644 --- a/lib/sof/array.rb +++ b/lib/sof/array.rb @@ -2,7 +2,7 @@ Array.class_eval do def to_sof_node(writer , level) node = Sof::ArrayNode.new() each do |object| - node.add writer.to_sof_node( object ) + node.add writer.to_sof_node( object , level + 1) end node end diff --git a/lib/sof/hash.rb b/lib/sof/hash.rb index d99e6693..963b2f0c 100644 --- a/lib/sof/hash.rb +++ b/lib/sof/hash.rb @@ -25,8 +25,8 @@ Hash.class_eval do def to_sof_node(writer , level) node = Sof::HashNode.new() each do |key , object| - k = writer.to_sof_node( key ) - v = writer.to_sof_node( object ) + k = writer.to_sof_node( key ,level + 1) + v = writer.to_sof_node( object ,level +1) node.add(k , v) end node diff --git a/lib/sof/writer.rb b/lib/sof/writer.rb index d1689c76..c907fe96 100644 --- a/lib/sof/writer.rb +++ b/lib/sof/writer.rb @@ -6,22 +6,25 @@ module Sof end def write - node = to_sof_node(@members.root) + node = to_sof_node(@members.root , 0) io = StringIO.new node.out( io , 0 ) io.string end - def to_sof_node(object) + def to_sof_node(object , level) if is_value?(object) return SimpleNode.new(object.to_sof()) end occurence = @members.objects[object] raise "no object #{object}" unless occurence + if(level > occurence.level ) + return SimpleNode.new("*#{occurence.number}") + end if(object.respond_to? :to_sof_node) #mainly meant for arrays and hashes - object.to_sof_node(self , occurence.level) + object.to_sof_node(self , level + 1) else - object_sof_node(object , occurence.level) + object_sof_node(object , level + 1 ) end end diff --git a/test/sof.rb b/test/sof.rb index f9d66690..f7e84c41 100644 --- a/test/sof.rb +++ b/test/sof.rb @@ -75,4 +75,10 @@ class BasicSof < MiniTest::Test @out = Sof::Writer.write({ one: [1 , ObjectWithAttributes.new] , two: true }) check "-:one: -1\n -#{OBJECT_STRING}\n-:two: true" end + def test_array_recursive + ar = [true, 1 ] + ar << ar + @out = Sof::Writer.write(ar) + check "-true\n-1\n-*1" + end end \ No newline at end of file