rubyx/lib/sof/writer.rb

51 lines
1.3 KiB
Ruby
Raw Normal View History

module Sof
class Writer
include Util
def initialize members
@members = members
end
def write
2014-08-18 12:22:03 +02:00
node = to_sof_node(@members.root , 0)
io = StringIO.new
node.out( io , 0 )
io.string
end
2014-08-18 12:22:03 +02:00
def to_sof_node(object , level)
if is_value?(object)
2014-08-16 11:34:25 +02:00
return SimpleNode.new(object.to_sof())
end
occurence = @members.objects[object]
raise "no object #{object}" unless occurence
2014-08-18 12:22:03 +02:00
if(level > occurence.level )
return SimpleNode.new("*#{occurence.number}")
end
if(object.respond_to? :to_sof_node) #mainly meant for arrays and hashes
2014-08-18 12:22:03 +02:00
object.to_sof_node(self , level + 1)
else
2014-08-18 12:22:03 +02:00
object_sof_node(object , level + 1 )
end
end
def object_sof_node( object , level)
head = object.class.name + "("
2014-08-18 11:49:38 +02:00
atts = attributes_for(object)
immediate , extended = atts.partition {|a| is_value?(get_value(object , a) ) }
2014-08-16 08:56:11 +02:00
head += immediate.collect {|a| "#{a}: #{get_value(object , a).to_sof()}"}.join(", ") + ")"
node = ObjectNode.new(head)
extended.each do |a|
val = get_value(object , a)
2014-08-18 11:49:38 +02:00
node.add( to_sof_node(a) , to_sof_node(val) )
end
node
end
def self.write object
writer = Writer.new(Members.new(object) )
writer.write
end
end
end