lib/spnet/ports/command_in_port.rb in spnet-0.1.4 vs lib/spnet/ports/command_in_port.rb in spnet-0.1.5
- old
+ new
@@ -1,26 +1,34 @@
-require 'hashmake'
-
module SPNet
+
+# Provides a means to list and execute commands in a Block object.
+#
+# @author James Tunnell
class CommandInPort < InPort
include Hashmake::HashMakeable
- ARG_SPECS = [
- Hashmake::ArgSpec.new(:key => :command_map, :reqd => true, :type => Proc, :container => Hashmake::ArgSpec::CONTAINER_HASH),
- ]
+ # Define arg specs to use in processing hashed arguments during #initialize.
+ ARG_SPECS = {
+ :command_map => arg_spec_hash(:reqd => true, :type => Proc, :validator => ->(a){a.arity == 1}),
+ }
+ # A new instance of CommandInPort.
+ # @param [Hash] hashed_args Hashed arguments for initialization. See Network::ARG_SPECS
+ # for details.
def initialize hashed_args
- hash_make(CommandInPort::ARG_SPECS, hashed_args)
- hashed_args.merge!(:matching_port_class => CommandOutPort)
- super(hashed_args)
+ hash_make CommandInPort::ARG_SPECS, hashed_args
+ super(:matching_class => CommandOutPort)
end
+ # List the commands that are available.
def list_commands
@command_map.keys
end
+ # Execute a command with the given data (nil by default).
def exec_command command, data
+ raise "Command #{command} not found in command list" unless @command_map.has_key?(command)
@command_map[command].call data
end
end
end