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