getting some sof output and adding some tests. issues though. abound

This commit is contained in:
Torsten Ruger
2014-08-14 17:40:56 +03:00
parent 7e60827dd3
commit 2c2ae14928
7 changed files with 104 additions and 23 deletions

View File

@ -1,3 +1,34 @@
require_relative "members"
require_relative "array"
require_relative "occurence"
Symbol.class_eval do
def to_sof(io, members)
io.write ":#{to_s}"
end
end
NilClass.class_eval do
def to_sof(io,members)
io.write "nil"
end
end
TrueClass.class_eval do
def to_sof(io , members)
io.write "true"
end
end
FalseClass.class_eval do
def to_sof(io , members)
io.write "false"
end
end
String.class_eval do
def to_sof(io, members)
io.write self
end
end
Fixnum.class_eval do
def to_sof(io , members)
io.write to_s
end
end

View File

@ -1,8 +1,13 @@
Array.class_eval do
def attributes
[]
def add_sof(members , level)
each do |o|
members.add(o , level + 1)
end
end
def to_sof
""
def to_sof(io , members)
each do |object|
io.write("\n")
members.output(io , object)
end
end
end

View File

@ -7,40 +7,62 @@ module Sof
@objects = {}
add(root ,0 )
end
attr_reader :objects
def add object , level
if( @objects.has_key?(object) )
occurence = @objects.get(object)
occurence = @objects[object]
occurence.level = level if occurence.level > level
else
o = Occurence.new( object , @counter , level )
@objects[object] = o
c = @counter
@counter = @counter + 1
object.attributes.each do a
val = object.send a
add(val , level + 1)
if( object.respond_to?(:attributes))
object.attributes.each do |a|
val = object.send a
add(val , level + 1)
end
elsif not value?(object)
object.add_sof(self , level)
end
end
end
def value? o
return true if o == true
return true if o == false
return true if o == nil
return true if o.class == Fixnum
return true if o.class == Symbol
return true if o.class == String
return false
end
def write
string = ""
output string , @root
io = StringIO.new
output io , @root
io.string
end
def output string , object
def output io , object
occurence = @objects[object]
raise "no object #{object}" unless occurence
indent = " " * occurence.level
string += indent
io.write indent
if(object.respond_to? :to_sof)
string += object.to_sof + "\n"
object.to_sof(io , self)
else
string += "!" + object.class.name + "\n"
indent += " "
object.attributes.each do a
val = object.send a
output( string , val)
io.write object.class.name
if( object.respond_to?(:attributes))
object.attributes.each do |a|
val = object.send a
io.write( a )
io.write( " " )
output( io , val)
end
io.puts ""
else
raise "General object not supported (yet), need attribute method #{object}"
end
end
end