From a594b716bcb82605bbdcee7fac3c885c3c0d0a72 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 18 Aug 2014 14:54:03 +0300 Subject: [PATCH] inlining references into objects as simple data --- lib/sof/all.rb | 5 +++++ lib/sof/writer.rb | 15 +++++++++------ test/sof.rb | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/sof/all.rb b/lib/sof/all.rb index 97f9986e..178bb0fd 100644 --- a/lib/sof/all.rb +++ b/lib/sof/all.rb @@ -17,6 +17,11 @@ TrueClass.class_eval do "true" end end +NilClass.class_eval do + def to_sof() + "nil" + end +end FalseClass.class_eval do def to_sof() "false" diff --git a/lib/sof/writer.rb b/lib/sof/writer.rb index 047d8a51..b27bf845 100644 --- a/lib/sof/writer.rb +++ b/lib/sof/writer.rb @@ -32,13 +32,16 @@ module Sof def object_sof_node( object , level , ref) head = object.class.name + "(" - atts = attributes_for(object) - immediate , extended = atts.partition {|a| is_value?(get_value(object , a) ) } - head += immediate.collect {|a| "#{a}: #{get_value(object , a).to_sof()}"}.join(", ") + ")" - node = ObjectNode.new(head , ref) - extended.each do |a| + atts = attributes_for(object).inject({}) do |hash , a| val = get_value(object , a) - node.add( to_sof_node(a,level + 1) , to_sof_node(val, level + 1) ) + hash[a] = to_sof_node(val , level + 1) + hash + end + immediate , extended = atts.partition {|a,val| val.is_a?(SimpleNode) } + head += immediate.collect {|a,val| "#{a}: #{val.data}"}.join(", ") + ")" + node = ObjectNode.new(head , ref) + extended.each do |a , val| + node.add( to_sof_node(a,level + 1) , val ) end node end diff --git a/test/sof.rb b/test/sof.rb index dd33cc7f..e1caa64a 100644 --- a/test/sof.rb +++ b/test/sof.rb @@ -84,6 +84,6 @@ class BasicSof < MiniTest::Test object = ObjectWithAttributes.new object.extra = object @out = Sof::Writer.write(object) - check "&1 #{OBJECT_STRING}\n :extra *1" + check "&1 ObjectWithAttributes(name: 'some name', number: 1234, extra: *1)" end end \ No newline at end of file