adds classes derived from array and hash
These are used in parfait and were coming out wrong should be fixed, but hash tests weak
This commit is contained in:
@ -25,7 +25,6 @@ module Sof
|
||||
short
|
||||
end
|
||||
|
||||
private
|
||||
# This defines the short output which is basically what you would write in ruby
|
||||
# ie [ value1 , value2 , ...]
|
||||
# The short is used for 7 or less SimpleNodes
|
||||
|
@ -22,7 +22,6 @@ module Sof
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
# This defines the short output which is basically what you would write in ruby
|
||||
# ie { key1 => value1 , ... }
|
||||
# The short is used for 7 or less SimpleNodes
|
||||
|
@ -50,16 +50,9 @@ module Sof
|
||||
|
||||
case object.class.name
|
||||
when "Array" , "Parfait::List"
|
||||
# and array values
|
||||
object.each do |a|
|
||||
add(a , level + 1)
|
||||
end
|
||||
add_array object , level
|
||||
when "Hash" , "Parfait::Dictionary"
|
||||
# and hash keys/values
|
||||
object.each do |a,b|
|
||||
add(a , level + 1)
|
||||
add(b , level + 1)
|
||||
end
|
||||
add_hash object , level
|
||||
else
|
||||
# and recursively add attributes
|
||||
attributes = attributes_for(object)
|
||||
@ -67,6 +60,27 @@ module Sof
|
||||
val = get_value( object , a)
|
||||
add(val , level + 1)
|
||||
end
|
||||
#TODO get all superclsses here, but this covers 99% so . . moving on
|
||||
superclasses = [object.class.superclass.name]
|
||||
if superclasses.include?( "Array") or superclasses.include?( "List")
|
||||
add_array object , level
|
||||
end
|
||||
if superclasses.include?( "Hash") or superclasses.include?( "Dictionary")
|
||||
add_hash object , level
|
||||
end
|
||||
end
|
||||
end
|
||||
# and hash keys/values
|
||||
def add_hash hash ,level
|
||||
hash.each do |a,b|
|
||||
add(a , level + 1)
|
||||
add(b , level + 1)
|
||||
end
|
||||
end
|
||||
# and array values
|
||||
def add_array array , level
|
||||
array.each do |a|
|
||||
add(a , level + 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -13,8 +13,13 @@ module Sof
|
||||
@name = name
|
||||
@simple = {}
|
||||
@complex = {}
|
||||
@super = nil # if derived from array or hash
|
||||
end
|
||||
|
||||
# super is a hash or array, if the class of object derives from Hash/Array
|
||||
def add_super s
|
||||
@super = s
|
||||
end
|
||||
# attributes hold key value pairs
|
||||
def add k , v
|
||||
raise "Key should be symbol not #{k}" unless k.is_a? Symbol
|
||||
@ -25,8 +30,15 @@ module Sof
|
||||
end
|
||||
end
|
||||
|
||||
# simple when no complex attributes and any
|
||||
# possible super is also simple
|
||||
def is_simple?
|
||||
true if( @referenced.nil? and @complex.empty? and head.length < 30 )
|
||||
if( @referenced.nil? and @complex.empty? and head.length < 30 )
|
||||
unless(@super and !@super.is_simple?)
|
||||
return true
|
||||
end
|
||||
end
|
||||
false
|
||||
end
|
||||
|
||||
# write out at the given level
|
||||
@ -41,10 +53,21 @@ module Sof
|
||||
io.write " "
|
||||
v.out(io , level + 1)
|
||||
end
|
||||
if(@super)
|
||||
io.write " "
|
||||
@super.long_out(io,level)
|
||||
end
|
||||
end
|
||||
|
||||
def short_out io , level
|
||||
io.write head
|
||||
if(@super)
|
||||
if( @super.is_simple? )
|
||||
@super.short_out(io,level)
|
||||
else
|
||||
@super.long_out(io,level)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def head
|
||||
|
@ -45,7 +45,7 @@ module Sof
|
||||
raise "no object #{object}" unless occurence
|
||||
#puts "#{level} ? #{occurence.level} : ref #{occurence.referenced}"
|
||||
if( occurence.referenced )
|
||||
puts "ref #{occurence.referenced} level #{level} at #{occurence.level}"
|
||||
#puts "ref #{occurence.referenced} level #{level} at #{occurence.level}"
|
||||
return SimpleNode.new("->#{occurence.referenced}") unless (level == occurence.level )
|
||||
if( occurence.written.nil? )
|
||||
occurence.written = true
|
||||
@ -73,6 +73,9 @@ module Sof
|
||||
# simple nodes are returned for small objects
|
||||
# small means only simple attributes and only 30 chars of them
|
||||
# object nodes are basically arrays (see there)
|
||||
#
|
||||
# objects may be derived from array/hash. In that case the ObjectNode gets a super
|
||||
# (either ArrayNode or HashNode)
|
||||
def object_to_sof_node( object , level , ref)
|
||||
node = ObjectNode.new(object.class.name , ref)
|
||||
attributes_for(object).each() do |a|
|
||||
@ -80,6 +83,14 @@ module Sof
|
||||
next if val.nil?
|
||||
node.add( a , to_sof_node( val , level + 1) )
|
||||
end
|
||||
#TODO get all superclsses here, but this covers 99% so . . moving on
|
||||
superclasses = [object.class.superclass.name]
|
||||
if superclasses.include?( "Array") or superclasses.include?( "List")
|
||||
node.add_super( array_to_sof_node(object , level , ref ) )
|
||||
end
|
||||
if superclasses.include?( "Hash") or superclasses.include?( "Dictionary")
|
||||
node.add_super( hash_to_sof_node(object , level , ref ) )
|
||||
end
|
||||
node
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user