2014-08-18 12:01:52 +02:00
|
|
|
module Sof
|
|
|
|
class HashNode < Node
|
2014-08-18 12:53:05 +02:00
|
|
|
def initialize ref
|
|
|
|
super(ref)
|
2014-08-18 12:01:52 +02:00
|
|
|
@children = []
|
|
|
|
end
|
|
|
|
attr_reader :children
|
|
|
|
def add key , val
|
|
|
|
@children << [key,val]
|
|
|
|
end
|
|
|
|
def out io , level = 0
|
2014-08-18 12:53:05 +02:00
|
|
|
super
|
2014-08-27 13:42:41 +02:00
|
|
|
short = true
|
|
|
|
children.each do |k,v|
|
|
|
|
short = false unless k.is_a?(SimpleNode)
|
|
|
|
short = false unless v.is_a?(SimpleNode)
|
|
|
|
end
|
|
|
|
if(short and children.length < 7 )
|
|
|
|
short_out(io,level)
|
|
|
|
else
|
|
|
|
long_out(io , level)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def short_out(io,level)
|
2014-08-27 13:46:34 +02:00
|
|
|
io.write("{")
|
|
|
|
children.each_with_index do |child , i|
|
|
|
|
key , val = child
|
|
|
|
key.out(io , level + 1)
|
|
|
|
io.write " => "
|
|
|
|
val.out(io , level + 1)
|
|
|
|
io.write ", " unless (i+1) == children.length
|
|
|
|
end
|
|
|
|
io.write("}")
|
2014-08-27 13:42:41 +02:00
|
|
|
end
|
|
|
|
def long_out io , level
|
2014-08-18 12:01:52 +02:00
|
|
|
indent = " " * level
|
2014-08-27 13:42:41 +02:00
|
|
|
children.each_with_index do |child , i|
|
2014-08-18 12:01:52 +02:00
|
|
|
key , val = child
|
|
|
|
io.write "\n#{indent}" unless i == 0
|
|
|
|
io.write "-"
|
|
|
|
key.out(io , level + 1)
|
2014-08-18 16:19:34 +02:00
|
|
|
io.write " => "
|
2014-08-18 12:01:52 +02:00
|
|
|
val.out(io , level + 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-15 16:14:49 +02:00
|
|
|
Hash.class_eval do
|
2014-08-18 12:53:05 +02:00
|
|
|
def to_sof_node(writer , level , ref)
|
|
|
|
node = Sof::HashNode.new(ref)
|
2014-08-16 11:34:25 +02:00
|
|
|
each do |key , object|
|
2014-08-18 12:22:03 +02:00
|
|
|
k = writer.to_sof_node( key ,level + 1)
|
|
|
|
v = writer.to_sof_node( object ,level +1)
|
2014-08-18 07:19:48 +02:00
|
|
|
node.add(k , v)
|
2014-08-15 16:14:49 +02:00
|
|
|
end
|
2014-08-16 14:16:07 +02:00
|
|
|
node
|
2014-08-15 16:14:49 +02:00
|
|
|
end
|
|
|
|
end
|
2014-08-18 12:01:52 +02:00
|
|
|
|