2014-08-15 21:29:48 +02:00
|
|
|
# We transform objects into a tree of nodes
|
|
|
|
|
|
|
|
module Sof
|
2014-08-16 11:34:25 +02:00
|
|
|
#abstract base class for nodes in the tree
|
2014-08-15 21:29:48 +02:00
|
|
|
class Node
|
2014-08-18 12:01:52 +02:00
|
|
|
include Util
|
2014-08-16 11:34:25 +02:00
|
|
|
# must be able to output to a stream
|
|
|
|
def out io ,level
|
|
|
|
raise "abstract #{self}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SimpleNode < Node
|
2014-08-16 14:16:07 +02:00
|
|
|
def initialize data
|
|
|
|
@data = data
|
2014-08-15 21:29:48 +02:00
|
|
|
end
|
2014-08-18 12:01:52 +02:00
|
|
|
attr_reader :data
|
2014-08-16 10:43:41 +02:00
|
|
|
def out io , level
|
2014-08-18 07:19:48 +02:00
|
|
|
io.write(data)
|
2014-08-16 10:43:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-18 07:19:48 +02:00
|
|
|
class ObjectNode < Node
|
|
|
|
def initialize data
|
|
|
|
@data = data
|
|
|
|
@children = []
|
|
|
|
end
|
2014-08-18 12:01:52 +02:00
|
|
|
attr_reader :children , :data
|
2014-08-18 07:19:48 +02:00
|
|
|
def add k , v
|
|
|
|
@children << [k,v]
|
|
|
|
end
|
|
|
|
def out io , level = 0
|
|
|
|
io.write(@data)
|
2014-08-18 11:49:38 +02:00
|
|
|
indent = " " * (level + 1)
|
2014-08-18 07:19:48 +02:00
|
|
|
@children.each_with_index do |child , i|
|
|
|
|
k , v = child
|
2014-08-18 11:49:38 +02:00
|
|
|
io.write "\n#{indent}"
|
|
|
|
k.out(io , level + 2)
|
|
|
|
io.write " "
|
|
|
|
v.out(io , level + 2)
|
2014-08-18 07:19:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-08-15 21:29:48 +02:00
|
|
|
end
|