Class: LocoBot::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/loco_bot/cli.rb,
lib/loco_bot/cli/command.rb,
lib/loco_bot/cli/command/base.rb,
lib/loco_bot/cli/command/left.rb,
lib/loco_bot/cli/command/move.rb,
lib/loco_bot/cli/command/hodor.rb,
lib/loco_bot/cli/command/place.rb,
lib/loco_bot/cli/command/right.rb,
lib/loco_bot/cli/command/report.rb

Overview

Provides a ‘command line’-like interface to interact with the API, in a limited way: only one Robot and Table instances are available.

Defined Under Namespace

Modules: Command

Constant Summary collapse

INPUT_SPLIT_REGEXP =

The regexp used to parse input.

/ |,|\s/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



18
19
20
21
# File 'lib/loco_bot/cli.rb', line 18

def initialize
  @robot = Robot.new
  @table = Table.new
end

Instance Attribute Details

#robotRobot (readonly)

Returns the Robot.

Returns:



12
13
14
# File 'lib/loco_bot/cli.rb', line 12

def robot
  @robot
end

#tableTable (readonly)

Returns the Table.

Returns:



12
# File 'lib/loco_bot/cli.rb', line 12

attr_reader :robot

Instance Method Details

#input!(input) ⇒ void

Note:

Available commands are:

  • PLACE X,Y,F: put the robot on the table in position X,Y and facing NORTH, SOUTH, EAST or WEST.

  • MOVE: move the robot one unit forward in the direction it is currently facing.

  • LEFT and RIGHT rotate the robot 90 degrees in the specified direction without changing its position.

  • REPORT announces the X,Y and F of the robot.

  • HODOR outputs a friendly greating.

This method returns an undefined value.

Reads input for commands.

Parameters:

  • input (String)

    the input



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/loco_bot/cli.rb', line 32

def input!(input)
  parsed_data = parse_input(input)

  return if parsed_data[:command].nil?

  command_class = Command.class_from_name(parsed_data[:command])

  if command_class.nil?
    puts "#{parsed_data[:command].upcase}: not a known command. Valid commands are #{CLI::Command.list.join(', ')}."
  else
    execute_command(command_class, parsed_data[:arguments])
  end
end