fixing array tests, seperated nodes into different types

This commit is contained in:
Torsten Ruger
2014-08-16 11:43:41 +03:00
parent 7f7a174554
commit 2f84c0dfa6
4 changed files with 35 additions and 18 deletions

View File

@ -1,6 +1,6 @@
Array.class_eval do
def to_sof_node(members , level)
node = Sof::Node.new(nil)
node = Sof::ChildrenNode.new(nil)
each do |object|
node.add members.to_sof_node( object )
end

View File

@ -5,22 +5,34 @@ module Sof
def initialize head
@head = head
end
attr_accessor :head , :children
attr_accessor :head
def out io , level
io.write(head) if head
end
end
class ChildrenNode < Node
def initialize head
super(head)
@children = []
end
attr_accessor :children
def add child
child = Node.new(child) if(child.is_a? String)
@children = [] if(@children.nil?)
@children << child
end
def out io , level = 0
io.write head
return unless @children
first = @children[0]
io.write " "
super
return if @children.empty?
first = @children.first
io.write "-"
first.out(io , level + 1)
indent = " " * level
@children.each do |child|
next if child == first # done already
@children.each_with_index do |child , i|
next if i == 0 # done already
io.write "\n"
io.write indent
io.write "-"
child.out(io , level + 1)

View File

@ -8,7 +8,7 @@ module Sof
def write
node = to_sof_node(@members.root)
io = StringIO.new
node.out( io )
node.out( io , 0 )
io.string
end