Class: TwistyPuzzles::CubeMoveParser

Inherits:
AbstractMoveParser show all
Defined in:
lib/twisty_puzzles/cube_move_parser.rb

Overview

Parser for cube moves.

Constant Summary collapse

REGEXP =
begin
  axes_part = "(?<axis_name>[#{AbstractMove::AXES.join}])"
  face_names = CubeConstants::FACE_NAMES.join
  fat_move_part =
    "(?<width>\\d*)(?<fat_face_name>[#{face_names}])w"
  normal_move_part = "(?<face_name>[#{face_names}])"
  downcased_face_names = face_names.downcase
  maybe_fat_maybe_slice_move_part =
    "(?<maybe_fat_face_maybe_slice_name>[#{downcased_face_names}])"
  slice_move_part =
    "(?<slice_index>\\d+)(?<slice_name>[#{downcased_face_names}])"
  mslice_move_part =
    "(?<mslice_name>[#{AbstractMove::SLICE_FACES.keys.join}])"
  move_part = "(?:#{axes_part}|" \
              "#{fat_move_part}|" \
              "#{normal_move_part}|" \
              "#{maybe_fat_maybe_slice_move_part}|" \
              "#{slice_move_part}|#{mslice_move_part})"
  direction_names =
    AbstractDirection::POSSIBLE_DIRECTION_NAMES.flatten
  direction_names.sort_by! { |e| -e.length }
  direction_part = "(?<direction>#{direction_names.join('|')})"
  Regexp.new("#{move_part}#{direction_part}")
end
MOVE_TYPE_CREATORS =
MoveTypeCreator.new(%i[axis_face direction], Rotation),
  MoveTypeCreator.new(%i[fat_face direction width], FatMove),
  MoveTypeCreator.new(%i[face direction], FatMove),
  MoveTypeCreator.new(%i[maybe_fat_face_maybe_slice_face direction], MaybeFatMaybeSliceMove),
  MoveTypeCreator.new(%i[slice_face direction slice_index], SliceMove),
  MoveTypeCreator.new(%i[mslice_face direction], MaybeFatMSliceMaybeInnerMSliceMove)
].freeze
INSTANCE =
CubeMoveParser.new

Instance Method Summary collapse

Methods inherited from AbstractMoveParser

#parse_move, #parse_named_captures

Instance Method Details

#move_type_creatorsObject



55
56
57
# File 'lib/twisty_puzzles/cube_move_parser.rb', line 55

def move_type_creators
  MOVE_TYPE_CREATORS
end

#parse_axis_face(axis_face_string) ⇒ Object



70
71
72
# File 'lib/twisty_puzzles/cube_move_parser.rb', line 70

def parse_axis_face(axis_face_string)
  Face::ELEMENTS[AbstractMove::AXES.index(axis_face_string)]
end

#parse_direction(direction_string) ⇒ Object



63
64
65
66
67
68
# File 'lib/twisty_puzzles/cube_move_parser.rb', line 63

def parse_direction(direction_string)
  value = AbstractDirection::POSSIBLE_DIRECTION_NAMES.index do |ds|
    ds.include?(direction_string)
  end + 1
  CubeDirection.new(value)
end

#parse_move_part(name, value) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/twisty_puzzles/cube_move_parser.rb', line 83

def parse_move_part(name, value)
  case name
  when 'axis_name' then parse_axis_face(value)
  when 'width' then parse_width(value)
  when 'slice_index' then Integer(value, 10)
  when 'fat_face_name', 'face_name' then Face.by_name(value)
  when 'maybe_fat_face_maybe_slice_name', 'slice_name'
    Face.by_name(value.upcase)
  when 'mslice_name'
    parse_mslice_face(value)
  when 'direction' then parse_direction(value)
  else raise
  end
end

#parse_mslice_face(mslice_name) ⇒ Object



74
75
76
# File 'lib/twisty_puzzles/cube_move_parser.rb', line 74

def parse_mslice_face(mslice_name)
  AbstractMove::SLICE_FACES[mslice_name]
end

#parse_part_key(name) ⇒ Object



59
60
61
# File 'lib/twisty_puzzles/cube_move_parser.rb', line 59

def parse_part_key(name)
  name.sub('_name', '_face').sub('face_face', 'face')
end

#parse_width(width_string) ⇒ Object



78
79
80
# File 'lib/twisty_puzzles/cube_move_parser.rb', line 78

def parse_width(width_string)
  width_string.empty? ? 2 : Integer(width_string, 10)
end

#regexpObject



51
52
53
# File 'lib/twisty_puzzles/cube_move_parser.rb', line 51

def regexp
  REGEXP
end