summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2012-05-10 17:43:07 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2012-05-10 17:43:07 +0200
commitc596b64931fec2db2c1a198b7124431b04fc673d (patch)
tree8b1b981bc58dc9d2ab7cc7f190998919d13eebdc /lib
parentbed8e682e80a810c66f309faf7865872aadc85cc (diff)
downloadedoors-ruby-c596b64931fec2db2c1a198b7124431b04fc673d.zip
edoors-ruby-c596b64931fec2db2c1a198b7124431b04fc673d.tar.gz
Particle: implement #to_json and #self.json_create
Diffstat (limited to 'lib')
-rw-r--r--lib/evendoors.rb1
-rw-r--r--lib/evendoors/particle.rb36
2 files changed, 36 insertions, 1 deletions
diff --git a/lib/evendoors.rb b/lib/evendoors.rb
index 0a9a941..9c2ae84 100644
--- a/lib/evendoors.rb
+++ b/lib/evendoors.rb
@@ -31,6 +31,7 @@ module EvenDoors
#
end
#
+require 'json'
require 'evendoors/particle'
require 'evendoors/spot'
require 'evendoors/room'
diff --git a/lib/evendoors/particle.rb b/lib/evendoors/particle.rb
index 111ce9a..f532be1 100644
--- a/lib/evendoors/particle.rb
+++ b/lib/evendoors/particle.rb
@@ -6,7 +6,7 @@ module EvenDoors
#
class Particle
#
- def initialize
+ def initialize o={}
@ts = Time.now # creation time
@src = nil # Spot where it's originated from
@dst = nil # Spot where it's heading to
@@ -19,6 +19,40 @@ module EvenDoors
@link_fields = [] # the fields used to generate the link value
@payload = {} # the actual data carried by this particle
@merged = [] # list of merged particles
+ #
+ if not o.empty?
+ @ts = Time.parse(o['ts']) if o['ts']
+ @room = o['room']
+ @door = o['door']
+ @action = o['action']
+ @payload = o['payload']||{}
+ @src = EvenDoors::Spin.spin.resolve o['src'] if o['src']
+ @dst = EvenDoors::Spin.spin.resolve o['dst'] if o['dst']
+ o['dsts'].each do |dst| add_dsts dst end if o['dsts']
+ set_link_fields *o['link_fields'] if o['link_fields']
+ o['merged'].each do |merged| merge! Particle.json_create(merged) end if o['merged']
+ end
+ end
+ #
+ def to_json *a
+ {
+ 'kls' => self.class.name,
+ 'ts' => @ts,
+ 'src' => (@src ? @src.path : nil ),
+ 'dst' => (@dst ? @dst.path : nil ),
+ 'room' => @room,
+ 'door' => @door,
+ 'action' => @action,
+ 'dsts' => @dsts,
+ 'link_fields' => @link_fields,
+ 'payload' => @payload,
+ 'merged' => @merged
+ }.to_json *a
+ end
+ #
+ def self.json_create o
+ raise EvenDoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name
+ self.new o
end
#
def reset!