Class: TwistyPuzzles::MoveTypeCreator
- Inherits:
-
Object
- Object
- TwistyPuzzles::MoveTypeCreator
- Defined in:
- lib/twisty_puzzles/move_type_creator.rb
Overview
Class for creating one move type from its parts. Helper class for parsing logic.
Instance Method Summary collapse
- #applies_to?(parsed_parts) ⇒ Boolean
- #create(parsed_parts) ⇒ Object
-
#initialize(capture_keys, move_class) ⇒ MoveTypeCreator
constructor
A new instance of MoveTypeCreator.
Constructor Details
#initialize(capture_keys, move_class) ⇒ MoveTypeCreator
Returns a new instance of MoveTypeCreator.
7 8 9 10 11 12 13 |
# File 'lib/twisty_puzzles/move_type_creator.rb', line 7 def initialize(capture_keys, move_class) raise TypeError unless move_class.is_a?(Class) raise TypeError unless capture_keys.all?(Symbol) @capture_keys = capture_keys.freeze @move_class = move_class end |
Instance Method Details
#applies_to?(parsed_parts) ⇒ Boolean
15 16 17 |
# File 'lib/twisty_puzzles/move_type_creator.rb', line 15 def applies_to?(parsed_parts) parsed_parts.keys.sort == @capture_keys.sort end |
#create(parsed_parts) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/twisty_puzzles/move_type_creator.rb', line 19 def create(parsed_parts) raise ArgumentError unless applies_to?(parsed_parts) fields = @capture_keys.map { |name| parsed_parts[name] } @move_class.new(*fields) end |