fix recursive arrays

This commit is contained in:
Torsten Ruger 2014-08-18 13:22:03 +03:00
parent 99e0c0db18
commit ad4aaaff8a
4 changed files with 16 additions and 7 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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