From 198e70b6bd3ac518bc055a68d0eedd000be326a6 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 15 Aug 2014 14:59:38 +0300 Subject: [PATCH] fix array with object --- lib/sof/README.md | 23 ++++++++++++++++++++++- lib/sof/writer.rb | 9 ++++----- test/sof.rb | 5 +++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/sof/README.md b/lib/sof/README.md index 3971eb78..7647bd75 100644 --- a/lib/sof/README.md +++ b/lib/sof/README.md @@ -1,9 +1,30 @@ +### Reading the code + +Knowing what's going on while coding salama is not so easy: Hnce the need to look at code dumps + +Hence the need for a code/object file format (remeber an oo program is just objects, some data, some code, all objects) + +I started with yaml, which is nice in that it has a solid implementation, reads and writes, handles arbitrary objects, +handles graphs and is a sort of readable text format. + +But the sort of started to get to me, because 1) it's way to verbose and 2) does not allow for (easy) ordering. +Also it was placing references in weird (first seen) places. + +To fix this i started on Sof, with an eye to expand it. + +The main starting goal was quite like yaml, but with + +- more text per line, specifically objects with simle attributes to have a constructor like syntax +- Shorter class names (no ruby/object or even ruby/struct stuff) +- references at the most shallow level +- a possibility to order attributes and specify attributes that should not be serialized + ### Salama Object File Ok, so we all heard about object files, it's the things compilers create so we don't have to have huge compiles and can link them later. -Much few know what they include, and that is not because they are not very useful, but rather very complicated. +Much fewer know what they include, and that is not because they are not very useful, but rather very complicated. An object machine must off course have it's own object files, because: diff --git a/lib/sof/writer.rb b/lib/sof/writer.rb index 5481f451..0b7630d7 100644 --- a/lib/sof/writer.rb +++ b/lib/sof/writer.rb @@ -18,16 +18,14 @@ module Sof end occurence = @members.objects[object] raise "no object #{object}" unless occurence - indent = " " * occurence.level - io.write indent if(object.respond_to? :to_sof) #mainly meant for arrays and hashes object.to_sof(io , self , occurence.level) else - object_write(object , io) + object_sof(object , io , occurence.level) end end - def object_write( object , io) + def object_sof( object , io , level) io.write object.class.name io.write "(" attributes = attributes_for(object) @@ -43,7 +41,8 @@ module Sof attributes.each_with_index do |a , i| val = get_value(object , a) next if is_value?(val) - io.puts " -" + io.write " " * (level+1) + io.write "-" io.write( a ) io.write( ": " ) output( io , val) diff --git a/test/sof.rb b/test/sof.rb index b5f184ee..fd14b62e 100644 --- a/test/sof.rb +++ b/test/sof.rb @@ -24,4 +24,9 @@ class BasicSof < MiniTest::Test out = Sof::Writer.write([true, 1234]) assert_equal "-true\n-1234\n" , out end + def test_array_with_object + out = Sof::Writer.write([true, 1234 , ObjectWithAttributes.new]) + assert_equal "-true\n-1234\n-ObjectWithAttributes(name: 'some object' ,number: 1234)\n\n" , out + end + end \ No newline at end of file