moving away from the attributes aproach, tests fixed
This commit is contained in:
parent
2c2ae14928
commit
0a33f940cf
@ -1,4 +1,5 @@
|
||||
require_relative "members"
|
||||
require_relative "writer"
|
||||
require_relative "array"
|
||||
require_relative "occurence"
|
||||
|
||||
|
@ -1,9 +1,4 @@
|
||||
Array.class_eval do
|
||||
def add_sof(members , level)
|
||||
each do |o|
|
||||
members.add(o , level + 1)
|
||||
end
|
||||
end
|
||||
def to_sof(io , members)
|
||||
each do |object|
|
||||
io.write("\n")
|
||||
|
13
lib/sof/known.rb
Normal file
13
lib/sof/known.rb
Normal file
@ -0,0 +1,13 @@
|
||||
module Sof
|
||||
class Known
|
||||
@@mapping = {
|
||||
MethodDefinition => [:name , :args , :receiver , :return_type , :blocks]
|
||||
}
|
||||
def self.is clazz
|
||||
@@mapping.has_key? clazz
|
||||
end
|
||||
def self.attributes clazz
|
||||
@@mapping[clazz]
|
||||
end
|
||||
end
|
||||
end
|
@ -7,29 +7,31 @@ module Sof
|
||||
@objects = {}
|
||||
add(root ,0 )
|
||||
end
|
||||
attr_reader :objects
|
||||
attr_reader :objects , :root
|
||||
|
||||
def add object , level
|
||||
return if Members.is_value?(object)
|
||||
if( @objects.has_key?(object) )
|
||||
occurence = @objects[object]
|
||||
occurence.level = level if occurence.level > level
|
||||
else
|
||||
return
|
||||
end
|
||||
o = Occurence.new( object , @counter , level )
|
||||
@objects[object] = o
|
||||
c = @counter
|
||||
@counter = @counter + 1
|
||||
if( object.respond_to?(:attributes))
|
||||
object.attributes.each do |a|
|
||||
val = object.send a
|
||||
attributes = attributes_for(object)
|
||||
attributes.each do |a|
|
||||
val = object.instance_variable_get "@#{a}".to_sym
|
||||
add(val , level + 1)
|
||||
end
|
||||
elsif not value?(object)
|
||||
object.add_sof(self , level)
|
||||
if( object.is_a? Array )
|
||||
object.each do |a|
|
||||
add(a , level + 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def value? o
|
||||
def self.is_value? o
|
||||
return true if o == true
|
||||
return true if o == false
|
||||
return true if o == nil
|
||||
@ -38,38 +40,13 @@ module Sof
|
||||
return true if o.class == String
|
||||
return false
|
||||
end
|
||||
def write
|
||||
io = StringIO.new
|
||||
output io , @root
|
||||
io.string
|
||||
end
|
||||
|
||||
def output io , object
|
||||
occurence = @objects[object]
|
||||
raise "no object #{object}" unless occurence
|
||||
indent = " " * occurence.level
|
||||
io.write indent
|
||||
if(object.respond_to? :to_sof)
|
||||
object.to_sof(io , self)
|
||||
def attributes_for object
|
||||
if( Known.is( object.class ))
|
||||
Known.attributes(object.class)
|
||||
else
|
||||
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}"
|
||||
object.instance_variables.collect{|i| i.to_s[1..-1].to_sym } # chop of @
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.write object
|
||||
members = Members.new object
|
||||
members.write
|
||||
end
|
||||
end
|
||||
end
|
||||
|
46
lib/sof/writer.rb
Normal file
46
lib/sof/writer.rb
Normal file
@ -0,0 +1,46 @@
|
||||
module Sof
|
||||
class Writer
|
||||
def initialize members
|
||||
@members = members
|
||||
end
|
||||
|
||||
def write
|
||||
io = StringIO.new
|
||||
output io , @members.root
|
||||
io.string
|
||||
end
|
||||
|
||||
def output io , object
|
||||
if Members.is_value?(object)
|
||||
object.to_sof(io , self)
|
||||
return
|
||||
end
|
||||
occurence = @members.objects[object]
|
||||
raise "no object #{object}" unless occurence
|
||||
indent = " " * occurence.level
|
||||
io.write indent
|
||||
if(object.respond_to? :to_sof)
|
||||
object.to_sof(io , self)
|
||||
else
|
||||
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
|
||||
|
||||
def self.write object
|
||||
writer = Writer.new(Members.new(object) )
|
||||
writer.write
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -34,9 +34,6 @@ module Virtual
|
||||
def MethodDefinition.main
|
||||
MethodDefinition.new(:main , [] , Virtual::SelfReference )
|
||||
end
|
||||
def attributes
|
||||
[:name , :args , :receiver , :return_type , :blocks]
|
||||
end
|
||||
def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Mystery , start = MethodEnter.new()
|
||||
@name = name.to_sym
|
||||
@args = args
|
||||
|
@ -6,12 +6,12 @@ end
|
||||
class BasicSof < MiniTest::Test
|
||||
|
||||
def test_true
|
||||
out = Sof::Members.write(true)
|
||||
assert_equal " true\n" , out
|
||||
out = Sof::Writer.write(true)
|
||||
assert_equal "true" , out
|
||||
end
|
||||
def test_num
|
||||
out = Sof::Members.write(124)
|
||||
assert_equal " 124\n" , out
|
||||
out = Sof::Writer.write(124)
|
||||
assert_equal "124" , out
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue
Block a user