class JustShogi::PieceFactory

Piece Factory

Generates pieces from a hash of arguments

Constants

CLASSES

A mapping of type descriptions to classes.

Public Class Methods

new(args) click to toggle source

New objects can be instantiated by passing in a hash or the piece.

@param [Hash,Piece] args

the initial attributes of the piece, hash requires type key

Example:

# Instantiates a new PieceFactory
JustShogi::PieceFactory.new({
  type: 'fuhyou',
  id: 1,
  player_number: 2
})
# File lib/just_shogi/piece_factory.rb, line 56
def initialize(args)
  @args = args
end

Public Instance Methods

build() click to toggle source

Returns a piece based on the initial arguments.

@return [Piece]

# File lib/just_shogi/piece_factory.rb, line 63
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

Private Instance Methods

build_from_hash() click to toggle source
# File lib/just_shogi/piece_factory.rb, line 78
def build_from_hash
  klass = CLASSES[@args[:type]]
  if klass
    klass.new(**@args)
  else
    raise ArgumentError, "invalid piece type: #{@args[:type].to_s}"
  end
end