fixing arrays/hashs and starting complex objects
This commit is contained in:
parent
9669831f78
commit
b0472753f4
@ -1,8 +1,8 @@
|
|||||||
Array.class_eval do
|
Array.class_eval do
|
||||||
def to_sof_node(members , level)
|
def to_sof_node(writer , level)
|
||||||
node = Sof::NodeList.new()
|
node = Sof::NodeList.new()
|
||||||
each do |object|
|
each do |object|
|
||||||
node.add members.to_sof_node( object )
|
node.add writer.to_sof_node( object )
|
||||||
end
|
end
|
||||||
node
|
node
|
||||||
end
|
end
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
Hash.class_eval do
|
Hash.class_eval do
|
||||||
def to_sof_node(members , level)
|
def to_sof_node(writer , level)
|
||||||
node = Sof::NodeList.new()
|
node = Sof::NodeList.new()
|
||||||
each do |key , object|
|
each do |key , object|
|
||||||
k = key.to_sof() + ": "
|
k = key.to_sof() + ": "
|
||||||
v = members.to_sof_node( object )
|
v = writer.to_sof_node( object )
|
||||||
v.data = "#{k}#{v.data}"
|
v.data = "#{k}#{v.data}"
|
||||||
node.add v
|
node.add v
|
||||||
end
|
end
|
||||||
|
@ -20,8 +20,8 @@ module Sof
|
|||||||
end
|
end
|
||||||
|
|
||||||
class NodeList < SimpleNode
|
class NodeList < SimpleNode
|
||||||
def initialize
|
def initialize data = nil
|
||||||
super(nil)
|
super(data)
|
||||||
@children = []
|
@children = []
|
||||||
end
|
end
|
||||||
attr_accessor :children
|
attr_accessor :children
|
||||||
|
@ -30,12 +30,13 @@ module Sof
|
|||||||
attributes = attributes_for(object)
|
attributes = attributes_for(object)
|
||||||
immediate , extended = attributes.partition {|a| is_value?(get_value(object , a) ) }
|
immediate , extended = attributes.partition {|a| is_value?(get_value(object , a) ) }
|
||||||
head += immediate.collect {|a| "#{a}: #{get_value(object , a).to_sof()}"}.join(", ") + ")"
|
head += immediate.collect {|a| "#{a}: #{get_value(object , a).to_sof()}"}.join(", ") + ")"
|
||||||
|
ex = {}
|
||||||
node = SimpleNode.new(head)
|
|
||||||
extended.each do |a|
|
extended.each do |a|
|
||||||
val = get_value(object , a)
|
val = get_value(object , a)
|
||||||
node.add to_sof_node(val)
|
ex[a] = val
|
||||||
end
|
end
|
||||||
|
node = ex.to_sof_node(self,level+1)
|
||||||
|
node.data = head
|
||||||
node
|
node
|
||||||
end
|
end
|
||||||
|
|
||||||
|
12
test/sof.rb
12
test/sof.rb
@ -6,6 +6,10 @@ class ObjectWithAttributes
|
|||||||
@name = "some name"
|
@name = "some name"
|
||||||
@number = 1234
|
@number = 1234
|
||||||
end
|
end
|
||||||
|
def extra_array
|
||||||
|
@extra = [:sym , 123]
|
||||||
|
self
|
||||||
|
end
|
||||||
end
|
end
|
||||||
OBJECT_STRING = "ObjectWithAttributes(name: 'some name', number: 1234)"
|
OBJECT_STRING = "ObjectWithAttributes(name: 'some name', number: 1234)"
|
||||||
|
|
||||||
@ -22,6 +26,10 @@ class BasicSof < MiniTest::Test
|
|||||||
out = Sof::Writer.write(ObjectWithAttributes.new)
|
out = Sof::Writer.write(ObjectWithAttributes.new)
|
||||||
assert_equal "#{OBJECT_STRING}" , out
|
assert_equal "#{OBJECT_STRING}" , out
|
||||||
end
|
end
|
||||||
|
def test_object_extra_array
|
||||||
|
out = Sof::Writer.write(ObjectWithAttributes.new.extra_array)
|
||||||
|
assert_equal "#{OBJECT_STRING}" , out
|
||||||
|
end
|
||||||
def test_simple_array
|
def test_simple_array
|
||||||
out = Sof::Writer.write([true, 1234])
|
out = Sof::Writer.write([true, 1234])
|
||||||
assert_equal "-true\n-1234" , out
|
assert_equal "-true\n-1234" , out
|
||||||
@ -54,6 +62,10 @@ class BasicSof < MiniTest::Test
|
|||||||
out = Sof::Writer.write({ one: 1 , two: ObjectWithAttributes.new })
|
out = Sof::Writer.write({ one: 1 , two: ObjectWithAttributes.new })
|
||||||
assert_equal "-:one: 1\n-:two: #{OBJECT_STRING}" , out
|
assert_equal "-:one: 1\n-:two: #{OBJECT_STRING}" , out
|
||||||
end
|
end
|
||||||
|
def test_array_hash
|
||||||
|
out = Sof::Writer.write([true, 1 , { one: 1 , tru: true }])
|
||||||
|
assert_equal "-true\n-1\n--:one: 1\n -:tru: true" , out
|
||||||
|
end
|
||||||
def test_hash_array
|
def test_hash_array
|
||||||
out = Sof::Writer.write({ one: [1 , ObjectWithAttributes.new] , two: true })
|
out = Sof::Writer.write({ one: [1 , ObjectWithAttributes.new] , two: true })
|
||||||
assert_equal "-:one: -1\n -#{OBJECT_STRING}\n-:two: true" , out
|
assert_equal "-:one: -1\n -#{OBJECT_STRING}\n-:two: true" , out
|
||||||
|
Loading…
Reference in New Issue
Block a user