summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/evendoors.rb1
-rw-r--r--lib/evendoors/particle.rb36
-rw-r--r--spec/particle_spec.rb50
-rw-r--r--spec/room_spec.rb1
4 files changed, 87 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!
diff --git a/spec/particle_spec.rb b/spec/particle_spec.rb
index a9ccd1d..41ef9a2 100644
--- a/spec/particle_spec.rb
+++ b/spec/particle_spec.rb
@@ -183,6 +183,56 @@ describe EvenDoors::Particle do
p.link_value.should eql 'v1'
end
#
+ it "json from to should work" do
+ EvenDoors::Spin.spin = nil
+ s0 = EvenDoors::Spin.new 'top'
+ s1 = EvenDoors::Room.new 'room0', s0
+ s2 = EvenDoors::Room.new 'room1', s1
+ s3 = EvenDoors::Door.new 'doora', s2
+ s4 = EvenDoors::Door.new 'doorb', s1
+ p0 = EvenDoors::Particle.new
+ p0['k0'] = 'v0'
+ p0['k1'] = 'v1'
+ p0['k2'] = 'v2'
+ p0.src = s3
+ p0.set_link_fields 'k0,k2'
+ p0.add_dsts 'room0/room1/room2/doorX?myaction,door?action,?action'
+ p0.split_dst!
+ p1 = EvenDoors::Particle.new
+ p1['k3'] = 'v6'
+ p1['k4'] = 'v7'
+ p1['k5'] = 'v8'
+ p1.src = s3
+ p1.dst_routed! s4
+ p1.set_link_fields 'k5,k4,k3'
+ p1.add_dsts 'room0/room1/door?action,output?action'
+ p0.merge! p1
+ px = EvenDoors::Particle.json_create( JSON.load( JSON.generate(p0) ) )
+ ((px.ts-p0.ts)<0.5).should be_true
+ px.src.should be s3
+ px.dst.should be_nil
+ px.room.should eql 'room0/room1/room2'
+ px.door.should eql 'doorX'
+ px.action.should eql 'myaction'
+ px.next_dst.should eql 'room0/room1/room2/doorX?myaction'
+ px.link_value.should eql 'v0v2'
+ px['k0'].should eql 'v0'
+ px['k1'].should eql 'v1'
+ px['k2'].should eql 'v2'
+ py = px.merged(0)
+ ((py.ts-p1.ts)<0.5).should be_true
+ py.src.should be s3
+ py.dst.should be s4
+ py.room.should be_nil
+ py.door.should be_nil
+ py.action.should be_nil
+ py.next_dst.should eql 'room0/room1/door?action'
+ py.link_value.should eql 'v8v7v6'
+ py['k3'].should eql 'v6'
+ py['k4'].should eql 'v7'
+ py['k5'].should eql 'v8'
+ end
+ #
end
#
# EOF
diff --git a/spec/room_spec.rb b/spec/room_spec.rb
index a683035..379de32 100644
--- a/spec/room_spec.rb
+++ b/spec/room_spec.rb
@@ -7,6 +7,7 @@ require 'spec_helper'
describe EvenDoors::Room do
#
it "add_spot and add_link correctly" do
+ EvenDoors::Spin.debug_routing = false
r0 = EvenDoors::Room.new 'room0', nil
d0 = EvenDoors::Door.new 'door0', r0
lambda { EvenDoors::Door.new('door0', r0) }.should raise_error(EvenDoors::Exception)