broaden the node hierachy
This commit is contained in:
parent
2f84c0dfa6
commit
90e4837b2e
@ -1,6 +1,6 @@
|
||||
Array.class_eval do
|
||||
def to_sof_node(members , level)
|
||||
node = Sof::ChildrenNode.new(nil)
|
||||
node = Sof::NodeList.new()
|
||||
each do |object|
|
||||
node.add members.to_sof_node( object )
|
||||
end
|
||||
|
@ -1,13 +1,9 @@
|
||||
Hash.class_eval do
|
||||
def to_sof_node(members , level)
|
||||
each_with_index do |pair , i|
|
||||
key , object = pair
|
||||
io.write(" " * level) unless i == 0
|
||||
io.write "-"
|
||||
members.output( io , key)
|
||||
io.write( " " )
|
||||
members.output( io , object)
|
||||
io.write("\n")
|
||||
node = Sof::NodeList.new(nil)
|
||||
each do |key , object|
|
||||
k = key.to_sof() + ": "
|
||||
v = members.to_sof_node( object )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,7 +1,15 @@
|
||||
# We transform objects into a tree of nodes
|
||||
|
||||
module Sof
|
||||
#abstract base class for nodes in the tree
|
||||
class Node
|
||||
# must be able to output to a stream
|
||||
def out io ,level
|
||||
raise "abstract #{self}"
|
||||
end
|
||||
end
|
||||
|
||||
class SimpleNode < Node
|
||||
def initialize head
|
||||
@head = head
|
||||
end
|
||||
@ -11,20 +19,18 @@ module Sof
|
||||
end
|
||||
end
|
||||
|
||||
class ChildrenNode < Node
|
||||
def initialize head
|
||||
super(head)
|
||||
class NodeList < Node
|
||||
def initialize
|
||||
@children = []
|
||||
end
|
||||
attr_accessor :children
|
||||
|
||||
def add child
|
||||
child = Node.new(child) if(child.is_a? String)
|
||||
child = SimpleNode.new(child) if(child.is_a? String)
|
||||
@children << child
|
||||
end
|
||||
|
||||
def out io , level = 0
|
||||
super
|
||||
return if @children.empty?
|
||||
first = @children.first
|
||||
io.write "-"
|
||||
|
@ -14,7 +14,7 @@ module Sof
|
||||
|
||||
def to_sof_node(object)
|
||||
if is_value?(object)
|
||||
return Node.new(object.to_sof())
|
||||
return SimpleNode.new(object.to_sof())
|
||||
end
|
||||
occurence = @members.objects[object]
|
||||
raise "no object #{object}" unless occurence
|
||||
@ -31,7 +31,7 @@ module Sof
|
||||
immediate , extended = attributes.partition {|a| is_value?(get_value(object , a) ) }
|
||||
head += immediate.collect {|a| "#{a}: #{get_value(object , a).to_sof()}"}.join(", ") + ")"
|
||||
|
||||
node = Node.new(head)
|
||||
node = SimpleNode.new(head)
|
||||
extended.each do |a|
|
||||
val = get_value(object , a)
|
||||
node.add to_sof_node(val)
|
||||
|
@ -42,13 +42,13 @@ class BasicSof < MiniTest::Test
|
||||
out = Sof::Writer.write([true, 1 , [true , 12 , [true , 123 ]]])
|
||||
assert_equal "-true\n-1\n--true\n -12\n --true\n -123" , out
|
||||
end
|
||||
def ttest_array_array_object
|
||||
def test_array_array_object
|
||||
out = Sof::Writer.write([true, 1 , [true , 12 , ObjectWithAttributes.new]])
|
||||
puts out
|
||||
assert_equal "-true\n-1\n--true\n -12\n -#{OBJECT_STRING}\n\n" , out
|
||||
assert_equal "-true\n-1\n--true\n -12\n -#{OBJECT_STRING}" , out
|
||||
end
|
||||
def ttest_simple_hash
|
||||
out = Sof::Writer.write({ one: 1 , tru: true })
|
||||
puts out
|
||||
assert_equal "-:one 1\n-:tru true\n" , out
|
||||
end
|
||||
def ttest_hash_object
|
||||
|
Loading…
Reference in New Issue
Block a user