Class: SPNet::CommandInPort

Inherits:
InPort
  • Object
show all
Includes:
Hashmake::HashMakeable
Defined in:
lib/spnet/ports/command_in_port.rb

Overview

Provides a means to list and execute commands in a Block object.

Author:

  • James Tunnell

Constant Summary collapse

ARG_SPECS =

Define arg specs to use in processing hashed arguments during #initialize.

{
  :command_map => arg_spec_hash(:reqd => true, :type => Proc, :validator => ->(a){a.arity == 1}),
}

Instance Attribute Summary

Attributes inherited from InPort

#link, #matching_class

Instance Method Summary collapse

Methods inherited from InPort

#clear_link, #linked?, #set_link

Constructor Details

#initialize(hashed_args) ⇒ CommandInPort

A new instance of CommandInPort.

Parameters:

  • hashed_args (Hash)

    Hashed arguments for initialization. See Network::ARG_SPECS for details.



18
19
20
21
# File 'lib/spnet/ports/command_in_port.rb', line 18

def initialize hashed_args
  hash_make hashed_args, CommandInPort::ARG_SPECS
  super(:matching_class => CommandOutPort)
end

Instance Method Details

#exec_command(command, data) ⇒ Object

Execute a command with the given data (nil by default).



29
30
31
32
# File 'lib/spnet/ports/command_in_port.rb', line 29

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

#list_commandsObject

List the commands that are available.



24
25
26
# File 'lib/spnet/ports/command_in_port.rb', line 24

def list_commands
  @command_map.keys
end