spnet 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,3 +19,7 @@ Replace messages and message ports with more specific ports, like value ports an
19
19
  Each kind of port can only connect to its opposite (e.g. SignaInPort to SignalOutPort).
20
20
  Instead of different messages for different actions, each kind of port will just have specific methods (e.g. SignalInPort#enqueue_values or ValueInPort#set_value).
21
21
  In Block, contain all InPort objects in @in_ports, and all OutPort objects in @out_ports.
22
+
23
+ === 0.1.4 / 2013-02-06
24
+
25
+ CommandInPort only requires :command_map instead of :list_commands_handler and :exec_command_handler. The command map is just a hash that maps command names to their execution handlers.
@@ -10,8 +10,8 @@ class Block
10
10
  HASHED_ARG_SPECS = [
11
11
  Hashmake::ArgSpec.new(:reqd => false, :key => :name, :type => String, :default => "UNNAMED"),
12
12
  Hashmake::ArgSpec.new(:reqd => false, :key => :algorithm, :type => Proc, :default => DO_NOTHING),
13
- Hashmake::ArgSpec.new(:reqd => false, :key => :in_ports, :type => InPort, :array => true, :default => ->(){ Array.new } ),
14
- Hashmake::ArgSpec.new(:reqd => false, :key => :out_ports, :type => OutPort, :array => true, :default => ->(){ Array.new }),
13
+ Hashmake::ArgSpec.new(:reqd => false, :key => :in_ports, :type => InPort, :container => Hashmake::ArgSpec::CONTAINER_ARRAY, :default => ->(){ Array.new } ),
14
+ Hashmake::ArgSpec.new(:reqd => false, :key => :out_ports, :type => OutPort, :container => Hashmake::ArgSpec::CONTAINER_ARRAY, :default => ->(){ Array.new }),
15
15
  ]
16
16
 
17
17
  def initialize args = {}
@@ -6,8 +6,7 @@ class CommandInPort < InPort
6
6
  include Hashmake::HashMakeable
7
7
 
8
8
  ARG_SPECS = [
9
- Hashmake::ArgSpec.new(:key => :list_commands_handler, :reqd => true, :type => Proc),
10
- Hashmake::ArgSpec.new(:key => :exec_command_handler, :reqd => true, :type => Proc)
9
+ Hashmake::ArgSpec.new(:key => :command_map, :reqd => true, :type => Proc, :container => Hashmake::ArgSpec::CONTAINER_HASH),
11
10
  ]
12
11
 
13
12
  def initialize hashed_args
@@ -17,11 +16,11 @@ class CommandInPort < InPort
17
16
  end
18
17
 
19
18
  def list_commands
20
- @list_commands_handler.call
19
+ @command_map.keys
21
20
  end
22
21
 
23
22
  def exec_command command, data
24
- @exec_command_handler.call command, data
23
+ @command_map[command].call data
25
24
  end
26
25
  end
27
26
  end
@@ -1,4 +1,4 @@
1
1
  module SPNet
2
2
  # spnet version
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.4"
4
4
  end
@@ -2,41 +2,28 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe SPNet::CommandInPort do
4
4
  before :each do
5
- @commands = ["add", "sub", "mul", "div"]
6
-
7
- list_commands_handler = lambda { return @commands }
8
-
9
- exec_command_handler = lambda do |command, data|
10
- x = data[0]
11
- y = data[1]
12
-
13
- case command
14
- when "add"
15
- return x + y
16
- when "sub"
17
- return x - y
18
- when "mul"
19
- return x * y
20
- when "div"
21
- return x / y
22
- end
23
- end
24
-
25
- @port = SPNet::CommandInPort.new :list_commands_handler => list_commands_handler, :exec_command_handler => exec_command_handler
5
+ @port = SPNet::CommandInPort.new(
6
+ :command_map => {
7
+ :add => lambda {|data| data[0] + data[1] },
8
+ :sub => lambda {|data| data[0] - data[1] },
9
+ :mul => lambda {|data| data[0] * data[1] },
10
+ :div => lambda {|data| data[0] / data[1] },
11
+ }
12
+ )
26
13
  end
27
14
 
28
15
  describe '#list_commands' do
29
16
  it 'should pass back the return value from the list_commands handler' do
30
- @port.list_commands.should eq(@commands)
17
+ @port.list_commands.should eq([:add, :sub, :mul, :div])
31
18
  end
32
19
  end
33
20
 
34
21
  describe '#exec_command' do
35
22
  it 'should pass the command and data to the exec_command handler, and pass back the return value' do
36
- @port.exec_command("add", [1,2]).should eq(3)
37
- @port.exec_command("sub", [5,4]).should eq(1)
38
- @port.exec_command("mul", [3,2]).should eq(6)
39
- @port.exec_command("div", [9,3]).should eq(3)
23
+ @port.exec_command(:add, [1,2]).should eq(3)
24
+ @port.exec_command(:sub, [5,4]).should eq(1)
25
+ @port.exec_command(:mul, [3,2]).should eq(6)
26
+ @port.exec_command(:div, [9,3]).should eq(3)
40
27
  end
41
28
  end
42
29
  end
@@ -2,27 +2,14 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe SPNet::ValueOutPort do
4
4
  before :each do
5
- @commands = ["add", "sub", "mul", "div"]
6
-
7
- list_commands_handler = lambda { return @commands }
8
-
9
- exec_command_handler = lambda do |command, data|
10
- x = data[0]
11
- y = data[1]
12
-
13
- case command
14
- when "add"
15
- return x + y
16
- when "sub"
17
- return x - y
18
- when "mul"
19
- return x * y
20
- when "div"
21
- return x / y
22
- end
23
- end
24
-
25
- @in_port = SPNet::CommandInPort.new :list_commands_handler => list_commands_handler, :exec_command_handler => exec_command_handler
5
+ @in_port = SPNet::CommandInPort.new(
6
+ :command_map => {
7
+ :add => lambda {|data| data[0] + data[1] },
8
+ :sub => lambda {|data| data[0] - data[1] },
9
+ :mul => lambda {|data| data[0] * data[1] },
10
+ :div => lambda {|data| data[0] / data[1] },
11
+ }
12
+ )
26
13
  @out_port = SPNet::CommandOutPort.new
27
14
  end
28
15
 
@@ -36,17 +23,17 @@ describe SPNet::ValueOutPort do
36
23
  describe '#list_commands' do
37
24
  it 'should pass back the return value from the list_commands handler' do
38
25
  @out_port.add_link @in_port
39
- @out_port.list_commands.should eq([@commands])
26
+ @out_port.list_commands.should eq([[:add, :sub, :mul, :div]])
40
27
  end
41
28
  end
42
29
 
43
30
  describe '#exec_command' do
44
31
  it 'should pass the command and data to the exec_command handler, and pass back the return value' do
45
32
  @out_port.add_link @in_port
46
- @out_port.exec_command("add", [1,2]).should eq([3])
47
- @out_port.exec_command("sub", [5,4]).should eq([1])
48
- @out_port.exec_command("mul", [3,2]).should eq([6])
49
- @out_port.exec_command("div", [9,3]).should eq([3])
33
+ @out_port.exec_command(:add, [1,2]).should eq([3])
34
+ @out_port.exec_command(:sub, [5,4]).should eq([1])
35
+ @out_port.exec_command(:mul, [3,2]).should eq([6])
36
+ @out_port.exec_command(:div, [9,3]).should eq([3])
50
37
  end
51
38
  end
52
39
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spnet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -178,7 +178,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
178
178
  version: '0'
179
179
  segments:
180
180
  - 0
181
- hash: -270931181
181
+ hash: -300528529
182
182
  required_rubygems_version: !ruby/object:Gem::Requirement
183
183
  none: false
184
184
  requirements:
@@ -187,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
187
187
  version: '0'
188
188
  segments:
189
189
  - 0
190
- hash: -270931181
190
+ hash: -300528529
191
191
  requirements: []
192
192
  rubyforge_project:
193
193
  rubygems_version: 1.8.23