2014-08-14 18:24:18 +02:00
|
|
|
module Sof
|
|
|
|
class Writer
|
2014-08-14 18:49:20 +02:00
|
|
|
include Util
|
2014-08-14 18:24:18 +02:00
|
|
|
def initialize members
|
|
|
|
@members = members
|
|
|
|
end
|
|
|
|
|
|
|
|
def write
|
2014-08-18 12:22:03 +02:00
|
|
|
node = to_sof_node(@members.root , 0)
|
2014-08-14 18:24:18 +02:00
|
|
|
io = StringIO.new
|
2014-08-16 10:43:41 +02:00
|
|
|
node.out( io , 0 )
|
2014-08-14 18:24:18 +02:00
|
|
|
io.string
|
|
|
|
end
|
|
|
|
|
2014-08-18 12:22:03 +02:00
|
|
|
def to_sof_node(object , level)
|
2014-08-14 18:49:20 +02:00
|
|
|
if is_value?(object)
|
2014-08-16 11:34:25 +02:00
|
|
|
return SimpleNode.new(object.to_sof())
|
2014-08-14 18:24:18 +02:00
|
|
|
end
|
|
|
|
occurence = @members.objects[object]
|
|
|
|
raise "no object #{object}" unless occurence
|
2014-08-18 12:22:03 +02:00
|
|
|
if(level > occurence.level )
|
2014-08-18 12:33:40 +02:00
|
|
|
#puts "level #{level} at #{occurence.level}"
|
2014-08-18 16:25:49 +02:00
|
|
|
return SimpleNode.new("*#{occurence.referenced}")
|
2014-08-18 12:22:03 +02:00
|
|
|
end
|
2014-08-18 16:25:49 +02:00
|
|
|
ref = occurence.referenced
|
2014-08-15 21:29:48 +02:00
|
|
|
if(object.respond_to? :to_sof_node) #mainly meant for arrays and hashes
|
2014-08-18 12:53:05 +02:00
|
|
|
object.to_sof_node(self , level , ref )
|
2014-08-14 18:24:18 +02:00
|
|
|
else
|
2014-08-18 12:53:05 +02:00
|
|
|
object_sof_node(object , level , ref )
|
2014-08-14 18:49:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-18 12:53:05 +02:00
|
|
|
def object_sof_node( object , level , ref)
|
2014-08-15 21:29:48 +02:00
|
|
|
head = object.class.name + "("
|
2014-08-18 13:54:03 +02:00
|
|
|
atts = attributes_for(object).inject({}) do |hash , a|
|
2014-08-14 18:49:20 +02:00
|
|
|
val = get_value(object , a)
|
2014-08-18 13:54:03 +02:00
|
|
|
hash[a] = to_sof_node(val , level + 1)
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
immediate , extended = atts.partition {|a,val| val.is_a?(SimpleNode) }
|
2014-08-18 16:16:18 +02:00
|
|
|
head += immediate.collect {|a,val| "#{a.to_sof()} => #{val.data}"}.join(", ") + ")"
|
2014-08-18 13:54:03 +02:00
|
|
|
node = ObjectNode.new(head , ref)
|
|
|
|
extended.each do |a , val|
|
|
|
|
node.add( to_sof_node(a,level + 1) , val )
|
2014-08-14 18:24:18 +02:00
|
|
|
end
|
2014-08-15 21:29:48 +02:00
|
|
|
node
|
2014-08-14 18:24:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.write object
|
|
|
|
writer = Writer.new(Members.new(object) )
|
|
|
|
writer.write
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|