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-15 21:29:48 +02:00
|
|
|
node = to_sof_node(@members.root)
|
2014-08-14 18:24:18 +02:00
|
|
|
io = StringIO.new
|
2014-08-15 21:29:48 +02:00
|
|
|
node.out( io )
|
2014-08-14 18:24:18 +02:00
|
|
|
io.string
|
|
|
|
end
|
|
|
|
|
2014-08-15 21:29:48 +02:00
|
|
|
def to_sof_node(object)
|
2014-08-14 18:49:20 +02:00
|
|
|
if is_value?(object)
|
2014-08-15 21:29:48 +02:00
|
|
|
return Node.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-15 21:29:48 +02:00
|
|
|
if(object.respond_to? :to_sof_node) #mainly meant for arrays and hashes
|
|
|
|
object.to_sof_node(self , occurence.level)
|
2014-08-14 18:24:18 +02:00
|
|
|
else
|
2014-08-15 21:29:48 +02:00
|
|
|
object_sof_node(object , occurence.level)
|
2014-08-14 18:49:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-15 21:29:48 +02:00
|
|
|
def object_sof_node( object , level)
|
|
|
|
head = object.class.name + "("
|
2014-08-14 18:49:20 +02:00
|
|
|
attributes = attributes_for(object)
|
2014-08-15 21:29:48 +02:00
|
|
|
immediate , extended = attributes.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(", ") + ")"
|
2014-08-15 21:29:48 +02:00
|
|
|
|
|
|
|
node = Node.new(head)
|
|
|
|
extended.each do |a|
|
2014-08-14 18:49:20 +02:00
|
|
|
val = get_value(object , a)
|
2014-08-15 21:29:48 +02:00
|
|
|
node.add to_sof_node(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
|