rubyx/lib/sof/writer.rb

59 lines
1.4 KiB
Ruby
Raw Normal View History

module Sof
class Writer
include Util
def initialize members
@members = members
end
def write
io = StringIO.new
output io , @members.root
io.string
end
def output io , object
if is_value?(object)
object.to_sof(io , self)
return
end
occurence = @members.objects[object]
raise "no object #{object}" unless occurence
if(object.respond_to? :to_sof) #mainly meant for arrays and hashes
2014-08-14 19:24:26 +02:00
object.to_sof(io , self , occurence.level)
else
2014-08-15 13:59:38 +02:00
object_sof(object , io , occurence.level)
end
end
2014-08-15 13:59:38 +02:00
def object_sof( object , io , level)
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)
2014-08-15 13:59:38 +02:00
io.write " " * (level+1)
io.write "-"
io.write( a )
io.write( ": " )
output( io , val)
end
end
def self.write object
writer = Writer.new(Members.new(object) )
writer.write
end
end
end