Class: Toy::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/toy/controller.rb

Constant Summary collapse

COMMANDS =
%w[STOP HELP PLACE MOVE LEFT RIGHT REPORT]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeController

Returns a new instance of Controller.



9
10
11
# File 'lib/toy/controller.rb', line 9

def initialize
  @robot = Toy::Robot.new
end

Instance Attribute Details

#robotObject (readonly)

Returns the value of attribute robot.



7
8
9
# File 'lib/toy/controller.rb', line 7

def robot
  @robot
end

Instance Method Details

#startObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/toy/controller.rb', line 13

def start
  while command = gets
    unless Toy::Controller::COMMANDS.include?(command.gsub(/^[A-Z]+/).first)
      puts 'Invalid command! Type HELP for list of available commands.' and next
    end

    case command
      when /STOP/
        break
      else
        output = execute(command.strip)
        puts output if output
    end          
  end
end