to and from basic types (json didn't work)

still not all, but enough for debug experiment to work
This commit is contained in:
Torsten Ruger 2015-07-12 10:00:29 +03:00
parent 95fbc3de1a
commit 34f8d2409b

View File

@ -7,25 +7,50 @@
# PS: compare is only for tests and should be factored out to there
Array.class_eval do
def to_basic
collect do |item|
item.to_basic
end
end
end
Symbol.class_eval do
def to_basic
to_s
end
end
String.class_eval do
def to_basic
to_s
end
end
module Ast
class Expression
def attributes
raise "abstract called for #{self}"
end
def to_json(*a)
data = {}
def to_basic()
data = { "class" => self.class.name }
attributes.each do |name|
data[name] = instance_variable_get(name)
val = instance_variable_get("@#{name}".to_sym)
res = val.to_basic
data[name] = res
end
puts data
{
"json_class" => self.class.name,
"data" => data
}.to_json(*a)
data
end
def self.json_create(o)
new(*o["data"].values)
def self.from_basic(hash)
clazz = hash.delete("class")
keys = hash.keys
klass = clazz.split("::").inject(Object) {|o,c| o.const_get(c)}
keys.delete("class")
values = keys.collect{|k| read_basic(hash[k]) }
klass.new(*values)
end
def self.read_basic val
return from_basic(val) if val.is_a?(Hash)
return val.collect{|i| from_basic(i)} if(val.is_a? Array )
val
end
def inspect