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
|
|
|
|
io = StringIO.new
|
|
|
|
output io , @members.root
|
|
|
|
io.string
|
|
|
|
end
|
|
|
|
|
|
|
|
def output io , object
|
2014-08-14 18:49:20 +02:00
|
|
|
if is_value?(object)
|
2014-08-14 18:24:18 +02:00
|
|
|
object.to_sof(io , self)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
occurence = @members.objects[object]
|
|
|
|
raise "no object #{object}" unless occurence
|
|
|
|
indent = " " * occurence.level
|
|
|
|
io.write indent
|
2014-08-14 18:49:20 +02:00
|
|
|
if(object.respond_to? :to_sof) #mainly meant for arrays and hashes
|
2014-08-14 18:24:18 +02:00
|
|
|
object.to_sof(io , self)
|
|
|
|
else
|
2014-08-14 18:49:20 +02:00
|
|
|
object_write(object , io)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def object_write( object , io)
|
|
|
|
io.write object.class.name
|
|
|
|
io.write "("
|
|
|
|
attributes = attributes_for(object)
|
|
|
|
attributes.each_with_index do |a , i|
|
|
|
|
val = get_value(object , a)
|
|
|
|
next unless is_value?(val)
|
|
|
|
io.write( a )
|
|
|
|
io.write( ": " )
|
|
|
|
output( io , val)
|
|
|
|
io.write(" ,") unless i == (attributes.length - 1)
|
|
|
|
end
|
|
|
|
io.puts ")"
|
|
|
|
attributes.each_with_index do |a , i|
|
|
|
|
val = get_value(object , a)
|
|
|
|
next if is_value?(val)
|
|
|
|
io.puts " -"
|
|
|
|
io.write( a )
|
|
|
|
io.write( ": " )
|
|
|
|
output( io , val)
|
2014-08-14 18:24:18 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.write object
|
|
|
|
writer = Writer.new(Members.new(object) )
|
|
|
|
writer.write
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|