summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2012-05-10 22:53:51 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2012-05-10 22:53:51 +0200
commit62cf22cd59f4933671e188fb8e550299617ba18e (patch)
treeb34022505bf08a4845aa9af0eafc63cc45eb5026 /lib
parent4a0e3178b90450d2ee65e04b74f7f3f8f0e2830c (diff)
downloadedoors-ruby-62cf22cd59f4933671e188fb8e550299617ba18e.zip
edoors-ruby-62cf22cd59f4933671e188fb8e550299617ba18e.tar.gz
Room: implement #to_json and #self.json_create
Diffstat (limited to 'lib')
-rw-r--r--lib/evendoors/room.rb23
1 files changed, 23 insertions, 0 deletions
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