Class: Minesweeper::Console::Parser::CommandParser
- Inherits:
-
Object
- Object
- Minesweeper::Console::Parser::CommandParser
- Defined in:
- lib/minesweeper/console/parser/command_parser.rb
Instance Method Summary collapse
-
#initialize(a_minefield) ⇒ CommandParser
constructor
A new instance of CommandParser.
- #parse(user_input) ⇒ Object
Constructor Details
#initialize(a_minefield) ⇒ CommandParser
Returns a new instance of CommandParser.
12 13 14 15 |
# File 'lib/minesweeper/console/parser/command_parser.rb', line 12 def initialize(a_minefield) raise ArgumentError if a_minefield.nil? @minefield = a_minefield end |
Instance Method Details
#parse(user_input) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 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 |