diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2012-05-10 17:43:07 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2012-05-10 17:43:07 +0200 |
commit | c596b64931fec2db2c1a198b7124431b04fc673d (patch) | |
tree | 8b1b981bc58dc9d2ab7cc7f190998919d13eebdc /lib/evendoors | |
parent | bed8e682e80a810c66f309faf7865872aadc85cc (diff) | |
download | edoors-ruby-c596b64931fec2db2c1a198b7124431b04fc673d.zip edoors-ruby-c596b64931fec2db2c1a198b7124431b04fc673d.tar.gz |
Particle: implement #to_json and #self.json_create
Diffstat (limited to 'lib/evendoors')
-rw-r--r-- | lib/evendoors/particle.rb | 36 |
1 files changed, 35 insertions, 1 deletions
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! |