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" "true"
end end
end end
NilClass.class_eval do
def to_sof()
"nil"
end
end
FalseClass.class_eval do FalseClass.class_eval do
def to_sof() def to_sof()
"false" "false"

View File

@ -32,13 +32,16 @@ module Sof
def object_sof_node( object , level , ref) def object_sof_node( object , level , ref)
head = object.class.name + "(" head = object.class.name + "("
atts = attributes_for(object) atts = attributes_for(object).inject({}) do |hash , a|
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|
val = get_value(object , 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 end
node node
end end

View File

@ -84,6 +84,6 @@ class BasicSof < MiniTest::Test
object = ObjectWithAttributes.new object = ObjectWithAttributes.new
object.extra = object object.extra = object
@out = Sof::Writer.write(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
end end