Sha256: e374dc0b6225313f940e7ca0fe8eba26aae31f50c74700f7aa4d8f27c18c9e15

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'just_xiangqi/pieces/jiang'
require 'just_xiangqi/pieces/ju'
require 'just_xiangqi/pieces/ma'
require 'just_xiangqi/pieces/pao'
require 'just_xiangqi/pieces/shi'
require 'just_xiangqi/pieces/xiang'
require 'just_xiangqi/pieces/zu'

module JustXiangqi

  # = Piece Factory
  #
  # Generates pieces from a hash of arguments
  class PieceFactory

    # A mapping of type descriptions to classes.
    CLASSES = {
      'jiang' => Jiang,
      'ju' => Ju,
      'ma' => Ma,
      'pao' => Pao,
      'shi' => Shi,
      'xiang' => Xiang,
      'zu' => Zu 
    }

    # 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
    #   JustXiangqi::PieceFactory.new({
    #     type: 'zu',
    #     id: 1,
    #     player_number: 2
    #   })
    def initialize(args)
      @args = args
    end

    # Returns a piece based on the initial arguments.
    #
    # @return [Piece]
    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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
just_xiangqi-0.1.0 lib/just_xiangqi/piece_factory.rb