From 90e4837b2e9104b5c0f685a4ab5dafe463fcd3c3 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 16 Aug 2014 12:34:25 +0300 Subject: [PATCH] broaden the node hierachy --- lib/sof/array.rb | 2 +- lib/sof/hash.rb | 12 ++++-------- lib/sof/node.rb | 16 +++++++++++----- lib/sof/writer.rb | 4 ++-- test/sof.rb | 6 +++--- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/sof/array.rb b/lib/sof/array.rb index 3c896923..318eea06 100644 --- a/lib/sof/array.rb +++ b/lib/sof/array.rb @@ -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 diff --git a/lib/sof/hash.rb b/lib/sof/hash.rb index 38d329a6..6ab77b59 100644 --- a/lib/sof/hash.rb +++ b/lib/sof/hash.rb @@ -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 diff --git a/lib/sof/node.rb b/lib/sof/node.rb index df6b4d65..059ebf58 100644 --- a/lib/sof/node.rb +++ b/lib/sof/node.rb @@ -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 "-" diff --git a/lib/sof/writer.rb b/lib/sof/writer.rb index a7b42523..6cd9e007 100644 --- a/lib/sof/writer.rb +++ b/lib/sof/writer.rb @@ -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) diff --git a/test/sof.rb b/test/sof.rb index 4e72235d..4607b777 100644 --- a/test/sof.rb +++ b/test/sof.rb @@ -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