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