to and from basic types (json didn't work)
still not all, but enough for debug experiment to work
This commit is contained in:
parent
95fbc3de1a
commit
34f8d2409b
@ -7,25 +7,50 @@
|
|||||||
|
|
||||||
# PS: compare is only for tests and should be factored out to there
|
# 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
|
module Ast
|
||||||
class Expression
|
class Expression
|
||||||
def attributes
|
def attributes
|
||||||
raise "abstract called for #{self}"
|
raise "abstract called for #{self}"
|
||||||
end
|
end
|
||||||
def to_json(*a)
|
def to_basic()
|
||||||
data = {}
|
data = { "class" => self.class.name }
|
||||||
attributes.each do |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
|
end
|
||||||
puts data
|
data
|
||||||
{
|
|
||||||
"json_class" => self.class.name,
|
|
||||||
"data" => data
|
|
||||||
}.to_json(*a)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.json_create(o)
|
def self.from_basic(hash)
|
||||||
new(*o["data"].values)
|
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
|
end
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
|
Loading…
x
Reference in New Issue
Block a user