Class: JustXiangqi::PieceFactory
- Inherits:
-
Object
- Object
- JustXiangqi::PieceFactory
- Defined in:
- lib/just_xiangqi/piece_factory.rb
Overview
Piece Factory
Generates pieces from a hash of arguments
Constant Summary collapse
- CLASSES =
A mapping of type descriptions to classes.
{ 'jiang' => Jiang, 'ju' => Ju, 'ma' => Ma, 'pao' => Pao, 'shi' => Shi, 'xiang' => Xiang, 'zu' => Zu }
Instance Method Summary collapse
-
#build ⇒ Piece
Returns a piece based on the initial arguments.
-
#initialize(args) ⇒ PieceFactory
constructor
New objects can be instantiated by passing in a hash or the piece.
Constructor Details
#initialize(args) ⇒ PieceFactory
New objects can be instantiated by passing in a hash or the piece.
Example:
# Instantiates a new PieceFactory
JustXiangqi::PieceFactory.new({
type: 'zu',
id: 1,
player_number: 2
})
39 40 41 |
# File 'lib/just_xiangqi/piece_factory.rb', line 39 def initialize(args) @args = args end |
Instance Method Details
#build ⇒ Piece
Returns a piece based on the initial arguments.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/just_xiangqi/piece_factory.rb', line 46 def build case @args when Hash build_from_hash when Piece @args when nil nil else raise ArgumentError, "piece must be Hash or NilClass" end end |