Sha256: f2124700d1fd47ea7ae3f4dfefddd5e1fc657cedc543af9d3826652b6e03ccea

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module ToyRoboSimulator
  # The console is responsible for initaing a command line interface for 
  # users to access the program.
  class Console
    def initialize
      puts ::MESSAGE
      @n = 0
      @robo = Robo.new
      print "#{format('%02d', @n)} > "
    end

    def watch
      command = STDIN.gets
      while command
        run(command)
        print "#{format('%02d', @n += 1)} > "
        command = STDIN.gets
      end
    end

    def run(command)
      args   = command.split(' ')
      action = args[0].gsub("\n", '').downcase
      case
      when ::AVAILABLE_COMMANDS.include?(action)
        process(action, args)
      when action == 'exit'
        exit_program
      when action == 'help'
        help
      else
        puts ::WARNING
      end
    end

    private

    def process(action, args)
      begin
        @robo.send(action.to_sym, *args[1..-1])
      rescue ArgumentError => e
        tip if e.message.include? 'wrong number of arguments'
      end
    end

    def tip
      puts TIP
    end

    def exit_program
      puts "\nThank You!\n\n"
      exit
    end

    def help
      puts HELP
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
toy_robo_simulator-0.1.0 lib/toy_robo_simulator/console.rb