inlining references into objects as simple data

This commit is contained in:
Torsten Ruger 2014-08-18 14:54:03 +03:00
parent a874bd49d2
commit a594b716bc
3 changed files with 15 additions and 7 deletions

View File

@ -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"

View File

@ -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

View File

@ -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