Class: LocoBot::CLI
- Inherits:
-
Object
- Object
- LocoBot::CLI
- 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
-
#robot ⇒ Robot
readonly
The Robot.
-
#table ⇒ Table
readonly
The Table.
Instance Method Summary collapse
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
-
#input!(input) ⇒ void
Reads input for commands.
Constructor Details
Instance Attribute Details
#robot ⇒ Robot (readonly)
Returns the Robot.
12 13 14 |
# File 'lib/loco_bot/cli.rb', line 12 def robot @robot end |
#table ⇒ Table (readonly)
Returns the Table.
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.
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 |