Class: TwistyPuzzles::AbstractMoveParser

Inherits:
Object
  • Object
show all
Defined in:
lib/twisty_puzzles/abstract_move_parser.rb

Overview

Base class for move parsers.

Direct Known Subclasses

CubeMoveParser, SkewbMoveParser

Instance Method Summary collapse

Instance Method Details

#move_type_creatorsObject

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/twisty_puzzles/abstract_move_parser.rb', line 18

def move_type_creators
  raise NotImplementedError
end

#parse_move(move_string) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/twisty_puzzles/abstract_move_parser.rb', line 31

def parse_move(move_string)
  match = move_string.match(regexp)
  if !match || !match.pre_match.empty? || !match.post_match.empty?
    raise ArgumentError, "Invalid move #{move_string}."
  end

  parsed_parts = parse_named_captures(match)
  move_type_creators.each do |parser|
    return parser.create(parsed_parts) if parser.applies_to?(parsed_parts)
  end
  raise ArgumentError, "No move type creator applies to #{parsed_parts}"
end

#parse_move_part(_name, _string) ⇒ Object

Raises:

  • (NotImplementedError)


14
15
16
# File 'lib/twisty_puzzles/abstract_move_parser.rb', line 14

def parse_move_part(_name, _string)
  raise NotImplementedError
end

#parse_named_captures(match) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/twisty_puzzles/abstract_move_parser.rb', line 22

def parse_named_captures(match)
  present_named_captures = match.named_captures.compact
  present_named_captures.to_h do |name, string|
    key = parse_part_key(name).to_sym
    value = parse_move_part(name, string)
    [key, value]
  end
end

#parse_part_key(_name) ⇒ Object

Raises:

  • (NotImplementedError)


10
11
12
# File 'lib/twisty_puzzles/abstract_move_parser.rb', line 10

def parse_part_key(_name)
  raise NotImplementedError
end

#regexpObject

Raises:

  • (NotImplementedError)


6
7
8
# File 'lib/twisty_puzzles/abstract_move_parser.rb', line 6

def regexp
  raise NotImplementedError
end