Sha256: ab8f86f8b96e410a952a3f6b0a7e5698c6beda0f7946a087e97ca133aded0b0e

Contents?: true

Size: 1004 Bytes

Versions: 3

Compression:

Stored size: 1004 Bytes

Contents

module SPNet

# Provides a means to list and execute commands in a Block object.
#
# @author James Tunnell
class CommandInPort < InPort
  
  include Hashmake::HashMakeable

  # 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
    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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spnet-0.1.7 lib/spnet/ports/command_in_port.rb
spnet-0.1.6 lib/spnet/ports/command_in_port.rb
spnet-0.1.5 lib/spnet/ports/command_in_port.rb