make class a value

and minor others
This commit is contained in:
Torsten Ruger 2015-06-16 18:51:30 +03:00
parent 6f9d4af3cd
commit 72fa26b00e
7 changed files with 15 additions and 12 deletions

View File

@ -13,6 +13,11 @@ require_relative "sof/array_node"
require_relative "sof/hash_node"
require_relative "sof/occurence"
Class.class_eval do
def to_sof
self.name
end
end
Symbol.class_eval do
def to_sof()
":#{to_s}"

View File

@ -17,7 +17,7 @@ module Sof
node
end
# A ArrayNode is a Node for a Array. See Node for when and how nodes are used in Sof.
# 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
# the arrays values.
#

View File

@ -22,8 +22,8 @@ module Sof
# write out at the given level
# level determines the indentation (level * space)
# write out the data and then the children (always key value on one line)
def out io , level = 0
# write out the data and then the children (always key value on one line)
def out io , level
super
io.write(@data)
indent = " " * (level + 1)

View File

@ -12,6 +12,7 @@ module Sof
@written = nil
end
def set_reference r
raise "was set #{@referenced}" if @referenced
@referenced = r
end
attr_reader :object , :referenced

View File

@ -7,7 +7,7 @@ module Sof
# hence int/bool/string etc are values
def is_value? o
return true if [true , false , nil].include?(o)
return true if [Fixnum, Symbol, String].include?(o.class)
return true if [Fixnum, Symbol, String, Class].include?(o.class)
if o.respond_to? :is_value?
return true if o.is_value?
end

View File

@ -45,12 +45,12 @@ module Sof
raise "no object #{object}" unless occurence
#puts "#{level} ? #{occurence.level} : ref #{occurence.referenced}"
if( occurence.referenced )
return SimpleNode.new("*#{occurence.referenced}") unless (level == occurence.level )
#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
else
return SimpleNode.new("*#{occurence.referenced}")
return SimpleNode.new("->#{occurence.referenced}")
end
end
ref = occurence.referenced
@ -66,9 +66,6 @@ module Sof
# 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)
if( object.is_a? Class )
return SimpleNode.new( object.name , ref )
end
head = object.class.name + "("
atts = {}
attributes_for(object).each() do |a|
@ -77,7 +74,7 @@ module Sof
atts[a] = to_sof_node(val , level + 1)
end
immediate , extended = atts.partition {|a,val| val.is_a?(SimpleNode) }
head += immediate.collect {|a,val| "#{a.to_sof()} => #{val.as_string(level)}"}.join(", ") + ")"
head += immediate.collect {|a,val| "#{a.to_sof()} => #{val.as_string(level + 1)}"}.join(", ") + ")"
return SimpleNode.new(head) if( ref.nil? and extended.empty? and head.length < 30 )
node = ObjectNode.new(head , ref)
extended.each do |a , val|

View File

@ -55,6 +55,6 @@ class ObjectSof < MiniTest::Test
object.extra = ObjectWithAttributes
ar = [object , ObjectWithAttributes]
@out = ar
check "- ObjectWithAttributes(:name => 'some name', :number => 1234, :extra => ->1)\n- &1 ObjectWithAttributes"
check "- ObjectWithAttributes(:name => 'some name', :number => 1234, :extra => ObjectWithAttributes)\n- ObjectWithAttributes"
end
end