shifting code about

writer creates all nodes (not delegated)
Check for class name, just as in members
as is_a will lead to wrong results
This commit is contained in:
Torsten Ruger 2015-06-18 17:52:28 +03:00
parent 78f0108166
commit f00364cd18
3 changed files with 37 additions and 41 deletions

View File

@ -1,21 +1,6 @@
# If a class defines to_sof_node it tells the write that it will generate Nodes itself
# this delegates to array_to_sof_node
Array.class_eval do
def to_sof_node(writer , level , ref )
Sof.array_to_sof_node(self , writer , level , ref )
end
end
module Sof
# Creates a ArrayNode (see there) for the Array.
# This mainly involves creating nodes for the children
def self.array_to_sof_node(array , writer , level , ref )
node = Sof::ArrayNode.new(ref)
array.each do |object|
node.add writer.to_sof_node( object , level + 1)
end
node
end
# A ArrayNode is a Node for an Array. See Node for when and how nodes are used in Sof.
# A ArrayNode has a list of children that hold the value node representations for

View File

@ -1,24 +1,4 @@
Hash.class_eval do
# If a class defines to_sof_node it tells the write that it will generate Nodes itself
# this delegates to hash_to_sof_node
def to_sof_node(writer , level , ref)
Sof.hash_to_sof_node( self , writer , level , ref)
end
end
module Sof
# Creates a HashNode (see there) for the Hash.
# This mainly involves creating nodes for key value pairs
def self.hash_to_sof_node(hash,writer , level , ref)
node = Sof::HashNode.new(ref)
hash.each do |key , object|
k = writer.to_sof_node( key ,level + 1)
v = writer.to_sof_node( object ,level + 1)
node.add(k , v)
end
node
end
# A HashNode is a Node for a Hash. See Node for when and how nodes are used in Sof.
# A HashNode has a list of children that hold the key/value node representations for
# the hashes keys and values.

View File

@ -35,7 +35,7 @@ module Sof
# Level is mainly needed for the indenting
# from the object we get the Occurence and decide wether a reference node is needed
# simple objects (with more inner structure) become SimpleNodes
# Any structured object bocomes a ObjectNode
# Any structured object becomes a ObjectNode
# Hash and Array create their own nodes via to_sof_node functions on the classes
def to_sof_node(object , level)
if is_value?(object)
@ -53,19 +53,27 @@ module Sof
return SimpleNode.new("->#{occurence.referenced}")
end
end
ref = occurence.referenced
if(object.respond_to? :to_sof_node) #mainly meant for arrays and hashes
object.to_sof_node(self , level , ref )
case object.class.name
when "Array" , "Parfait::List"
# If a class defines to_sof_node it tells the write that it will generate Nodes itself
# this delegates to array_to_sof_node
array_to_sof_node(object , level , ref )
when "Hash" , "Parfait::Dictionary"
# and hash keys/values
hash_to_sof_node( object , level , ref)
else
object_sof_node(object , level , ref )
object_to_sof_node(object , level , ref)
end
end
# create an object node from the object
# 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)
def object_sof_node( object , level , ref)
def object_to_sof_node( object , level , ref)
node = ObjectNode.new(object.class.name , ref)
attributes_for(object).each() do |a|
val = get_value(object , a)
@ -74,5 +82,28 @@ module Sof
end
node
end
# Creates a ArrayNode (see there) for the Array.
# This mainly involves creating nodes for the children
def array_to_sof_node(array , level , ref )
node = Sof::ArrayNode.new(ref)
array.each do |object|
node.add to_sof_node( object , level + 1)
end
node
end
# Creates a HashNode (see there) for the Hash.
# This mainly involves creating nodes for key value pairs
def hash_to_sof_node(hash , level , ref)
node = Sof::HashNode.new(ref)
hash.each do |key , object|
k = to_sof_node( key ,level + 1)
v = to_sof_node( object ,level + 1)
node.add(k , v)
end
node
end
end
end