add transformation step to tree before output to help with the logic
This commit is contained in:
parent
b0a54cb8c2
commit
a10321922e
@ -5,31 +5,30 @@ require_relative "writer"
|
|||||||
require_relative "array"
|
require_relative "array"
|
||||||
require_relative "hash"
|
require_relative "hash"
|
||||||
require_relative "occurence"
|
require_relative "occurence"
|
||||||
|
require_relative "node"
|
||||||
|
|
||||||
Symbol.class_eval do
|
Symbol.class_eval do
|
||||||
def to_sof(io, members)
|
def to_sof()
|
||||||
io.write ":#{to_s}"
|
":#{to_s}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
TrueClass.class_eval do
|
TrueClass.class_eval do
|
||||||
def to_sof(io , members)
|
def to_sof()
|
||||||
io.write "true"
|
"true"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
FalseClass.class_eval do
|
FalseClass.class_eval do
|
||||||
def to_sof(io , members)
|
def to_sof()
|
||||||
io.write "false"
|
"false"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
String.class_eval do
|
String.class_eval do
|
||||||
def to_sof(io, members)
|
def to_sof()
|
||||||
io.write "'"
|
"'" + self + "'"
|
||||||
io.write self
|
|
||||||
io.write "'"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Fixnum.class_eval do
|
Fixnum.class_eval do
|
||||||
def to_sof(io , members)
|
def to_sof()
|
||||||
io.write to_s
|
to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
Array.class_eval do
|
Array.class_eval do
|
||||||
def to_sof(io , members , level)
|
def to_sof_node(members , level)
|
||||||
each_with_index do |object , i|
|
node = Sof::Node.new(nil)
|
||||||
io.write(" " * level) unless i == 0
|
each do |object|
|
||||||
io.write("-")
|
node.add members.to_sof_node( object )
|
||||||
members.output(io , object)
|
end
|
||||||
io.write("\n")
|
node
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
Hash.class_eval do
|
Hash.class_eval do
|
||||||
def to_sof(io , members , level)
|
def to_sof_node(members , level)
|
||||||
each_with_index do |pair , i|
|
each_with_index do |pair , i|
|
||||||
key , object = pair
|
key , object = pair
|
||||||
io.write(" " * level) unless i == 0
|
io.write(" " * level) unless i == 0
|
||||||
|
30
lib/sof/node.rb
Normal file
30
lib/sof/node.rb
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
# We transform objects into a tree of nodes
|
||||||
|
|
||||||
|
module Sof
|
||||||
|
class Node
|
||||||
|
def initialize head
|
||||||
|
@head = head
|
||||||
|
end
|
||||||
|
attr_accessor :head , :children
|
||||||
|
def add child
|
||||||
|
child = Node.new(child) if(child.is_a? String)
|
||||||
|
@children = [] if(@children.nil?)
|
||||||
|
@children << child
|
||||||
|
end
|
||||||
|
|
||||||
|
def out io , level = 0
|
||||||
|
io.write head
|
||||||
|
return unless @children
|
||||||
|
first = @children[0]
|
||||||
|
io.write " "
|
||||||
|
first.out(io , level + 1)
|
||||||
|
indent = " " * level
|
||||||
|
@children.each do |child|
|
||||||
|
next if child == first # done already
|
||||||
|
io.write indent
|
||||||
|
io.write "-"
|
||||||
|
child.out(io , level + 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -6,47 +6,37 @@ module Sof
|
|||||||
end
|
end
|
||||||
|
|
||||||
def write
|
def write
|
||||||
|
node = to_sof_node(@members.root)
|
||||||
io = StringIO.new
|
io = StringIO.new
|
||||||
output io , @members.root
|
node.out( io )
|
||||||
io.string
|
io.string
|
||||||
end
|
end
|
||||||
|
|
||||||
def output io , object
|
def to_sof_node(object)
|
||||||
if is_value?(object)
|
if is_value?(object)
|
||||||
object.to_sof(io , self)
|
return Node.new(object.to_sof())
|
||||||
return
|
|
||||||
end
|
end
|
||||||
occurence = @members.objects[object]
|
occurence = @members.objects[object]
|
||||||
raise "no object #{object}" unless occurence
|
raise "no object #{object}" unless occurence
|
||||||
if(object.respond_to? :to_sof) #mainly meant for arrays and hashes
|
if(object.respond_to? :to_sof_node) #mainly meant for arrays and hashes
|
||||||
object.to_sof(io , self , occurence.level)
|
object.to_sof_node(self , occurence.level)
|
||||||
else
|
else
|
||||||
object_sof(object , io , occurence.level)
|
object_sof_node(object , occurence.level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def object_sof( object , io , level)
|
def object_sof_node( object , level)
|
||||||
io.write object.class.name
|
head = object.class.name + "("
|
||||||
io.write "("
|
|
||||||
attributes = attributes_for(object)
|
attributes = attributes_for(object)
|
||||||
attributes.each_with_index do |a , i|
|
immediate , extended = attributes.partition {|a| is_value?(get_value(object , a) ) }
|
||||||
|
head += immediate.collect {|a| "#{a}: #{get_value(object , a)}"}.join(", ") + ")"
|
||||||
|
|
||||||
|
node = Node.new(head)
|
||||||
|
extended.each do |a|
|
||||||
val = get_value(object , a)
|
val = get_value(object , a)
|
||||||
next unless is_value?(val)
|
node.add to_sof_node(val)
|
||||||
io.write( a )
|
|
||||||
io.write( ": " )
|
|
||||||
output( io , val)
|
|
||||||
io.write(" ,") unless i == (attributes.length - 1)
|
|
||||||
end
|
|
||||||
io.write ")"
|
|
||||||
attributes.each_with_index do |a , i|
|
|
||||||
val = get_value(object , a)
|
|
||||||
next if is_value?(val)
|
|
||||||
io.write " " * (level+1)
|
|
||||||
io.write "-"
|
|
||||||
io.write( a )
|
|
||||||
io.write( ": " )
|
|
||||||
output( io , val)
|
|
||||||
end
|
end
|
||||||
|
node
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.write object
|
def self.write object
|
||||||
|
Loading…
Reference in New Issue
Block a user