class Minesweeper::Console::Parser::CommandParser
Public Class Methods
new(a_minefield)
click to toggle source
# File lib/minesweeper/console/parser/command_parser.rb, line 12 def initialize(a_minefield) raise ArgumentError if a_minefield.nil? @minefield = a_minefield end
Public Instance Methods
parse(user_input)
click to toggle source
# File lib/minesweeper/console/parser/command_parser.rb, line 17 def parse(user_input) if user_input.nil? || user_input =~ /^\s*$/ return NullCommand.new(@minefield) end validate_user_input(user_input) tokens = user_input.split(/\s+/) case tokens[0].upcase when 'R', 'REVEAL' RevealCommand.new(@minefield, *tokens[1,2].map(&:to_i)) when 'F', 'FLAG' FlagCommand.new(@minefield, *tokens[1,2].map(&:to_i)) when 'U', 'UNFLAG' UnflagCommand.new(@minefield, *tokens[1,2].map(&:to_i)) end end
Private Instance Methods
validate_user_input(user_input)
click to toggle source
# File lib/minesweeper/console/parser/command_parser.rb, line 33 def validate_user_input(user_input) if user_input !~ /^[RUFruf] .*$/ raise UnsupportedCommandError, 'Supported commands are r,u,f,R,U,F.' end if user_input !~ /^.*\d+ \d+$/ raise InvalidCommandParametersError, 'A command must be followed by two integers.' end end