rewrote write to use known attributes or instance variables, simple values inline
This commit is contained in:
parent
7eeb269d50
commit
6b19b915a1
@ -1,4 +1,6 @@
|
|||||||
|
require_relative "util"
|
||||||
require_relative "members"
|
require_relative "members"
|
||||||
|
require_relative "known"
|
||||||
require_relative "writer"
|
require_relative "writer"
|
||||||
require_relative "array"
|
require_relative "array"
|
||||||
require_relative "occurence"
|
require_relative "occurence"
|
||||||
@ -25,7 +27,9 @@ FalseClass.class_eval do
|
|||||||
end
|
end
|
||||||
String.class_eval do
|
String.class_eval do
|
||||||
def to_sof(io, members)
|
def to_sof(io, members)
|
||||||
|
io.write "'"
|
||||||
io.write self
|
io.write self
|
||||||
|
io.write "'"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Fixnum.class_eval do
|
Fixnum.class_eval do
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
module Sof
|
module Sof
|
||||||
class Writer
|
class Writer
|
||||||
|
include Util
|
||||||
def initialize members
|
def initialize members
|
||||||
@members = members
|
@members = members
|
||||||
end
|
end
|
||||||
@ -11,7 +12,7 @@ module Sof
|
|||||||
end
|
end
|
||||||
|
|
||||||
def output io , object
|
def output io , object
|
||||||
if Members.is_value?(object)
|
if is_value?(object)
|
||||||
object.to_sof(io , self)
|
object.to_sof(io , self)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -19,21 +20,33 @@ module Sof
|
|||||||
raise "no object #{object}" unless occurence
|
raise "no object #{object}" unless occurence
|
||||||
indent = " " * occurence.level
|
indent = " " * occurence.level
|
||||||
io.write indent
|
io.write indent
|
||||||
if(object.respond_to? :to_sof)
|
if(object.respond_to? :to_sof) #mainly meant for arrays and hashes
|
||||||
object.to_sof(io , self)
|
object.to_sof(io , self)
|
||||||
else
|
else
|
||||||
|
object_write(object , io)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def object_write( object , io)
|
||||||
io.write object.class.name
|
io.write object.class.name
|
||||||
if( object.respond_to?(:attributes))
|
io.write "("
|
||||||
object.attributes.each do |a|
|
attributes = attributes_for(object)
|
||||||
val = object.send a
|
attributes.each_with_index do |a , i|
|
||||||
|
val = get_value(object , a)
|
||||||
|
next unless is_value?(val)
|
||||||
io.write( a )
|
io.write( a )
|
||||||
io.write( " " )
|
io.write( ": " )
|
||||||
output( io , val)
|
output( io , val)
|
||||||
|
io.write(" ,") unless i == (attributes.length - 1)
|
||||||
end
|
end
|
||||||
io.puts ""
|
io.puts ")"
|
||||||
else
|
attributes.each_with_index do |a , i|
|
||||||
raise "General object not supported (yet), need attribute method #{object}"
|
val = get_value(object , a)
|
||||||
end
|
next if is_value?(val)
|
||||||
|
io.puts " -"
|
||||||
|
io.write( a )
|
||||||
|
io.write( ": " )
|
||||||
|
output( io , val)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
11
test/sof.rb
11
test/sof.rb
@ -1,6 +1,10 @@
|
|||||||
require_relative "helper"
|
require_relative "helper"
|
||||||
|
|
||||||
class SimpleObjectWithAttributes
|
class ObjectWithAttributes
|
||||||
|
def initialize
|
||||||
|
@name = "some object"
|
||||||
|
@number = 1234
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class BasicSof < MiniTest::Test
|
class BasicSof < MiniTest::Test
|
||||||
@ -13,5 +17,8 @@ class BasicSof < MiniTest::Test
|
|||||||
out = Sof::Writer.write(124)
|
out = Sof::Writer.write(124)
|
||||||
assert_equal "124" , out
|
assert_equal "124" , out
|
||||||
end
|
end
|
||||||
|
def test_object
|
||||||
|
out = Sof::Writer.write(ObjectWithAttributes.new)
|
||||||
|
assert_equal " ObjectWithAttributes(name: 'some object' ,number: 1234)\n" , out
|
||||||
|
end
|
||||||
end
|
end
|
Loading…
x
Reference in New Issue
Block a user