rubyx/lib/sof/hash.rb

38 lines
776 B
Ruby
Raw Normal View History

2014-08-18 13:01:52 +03:00
module Sof
class HashNode < Node
def initialize ref
super(ref)
2014-08-18 13:01:52 +03:00
@children = []
end
attr_reader :children
def add key , val
@children << [key,val]
end
def out io , level = 0
super
2014-08-18 13:01:52 +03:00
indent = " " * level
@children.each_with_index do |child , i|
key , val = child
io.write "\n#{indent}" unless i == 0
io.write "-"
key.out(io , level + 1)
io.write " => "
2014-08-18 13:01:52 +03:00
val.out(io , level + 1)
end
end
end
end
2014-08-15 17:14:49 +03:00
Hash.class_eval do
def to_sof_node(writer , level , ref)
node = Sof::HashNode.new(ref)
2014-08-16 12:34:25 +03:00
each do |key , object|
2014-08-18 13:22:03 +03:00
k = writer.to_sof_node( key ,level + 1)
v = writer.to_sof_node( object ,level +1)
node.add(k , v)
2014-08-15 17:14:49 +03:00
end
2014-08-16 15:16:07 +03:00
node
2014-08-15 17:14:49 +03:00
end
end
2014-08-18 13:01:52 +03:00