From 62cf22cd59f4933671e188fb8e550299617ba18e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 10 May 2012 22:53:51 +0200 Subject: Room: implement #to_json and #self.json_create --- lib/evendoors/room.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/evendoors/room.rb b/lib/evendoors/room.rb index ce53602..151cd8a 100644 --- a/lib/evendoors/room.rb +++ b/lib/evendoors/room.rb @@ -13,6 +13,29 @@ module EvenDoors @parent.add_spot self if @parent end # + def to_json *a + { + 'kls' => self.class.name, + 'name' => @name, + 'spots' => @spots, + 'links' => @links + }.to_json *a + end + # + def self.json_create o + raise EvenDoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name + room = self.new o['name'] + o['spots'].each do |name,spot| + room.add_spot eval( spot['kls'] ).json_create(spot) + end + o['links'].each do |src,links| + links.each do |link| + room.add_link EvenDoors::Link.json_create(link) + end + end + room + end + # def add_spot s raise EvenDoors::Exception.new "Spot #{s.name} already has #{s.parent.name} as parent" if not s.parent.nil? and s.parent!=self raise EvenDoors::Exception.new "Spot #{s.name} already exists in #{path}" if @spots.has_key? s.name -- cgit v1.1-2-g2b99