Sha256: 26f6811f4c2dbc313b71fcfd467ab5eeebb2f4f82c417c4f9173e67eecc0dba9

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

require "colorize"

module FiftyTwo
  class Card
    include Comparable

    attr_reader :deck, :rank, :suit
    delegate :color, to: :suit

    def initialize(deck, rank, suit)
      @deck = deck
      @rank = rank
      @suit = suit
    end

    def code
      "#{rank.code}#{suit.code}"
    end

    def to_s
      "#{rank} of #{suit}"
    end
    alias_method :name, :to_s

    def identifier
      "#{rank.identifier}#{suit.identifier}"
    end

    def render
      code.rjust(3).send(suit.color.name)
    end

    def <=>(other)
      [rank, suit] <=> [other.rank, other.suit]
    end

    def method_missing(name, **args, &block)
      return super unless name.to_s.ends_with?("?")

      attributes.each do |attribute|
        return attribute.send(name) if attribute.respond_to?(name)
      end

      super
    end

    def respond_to_missing?(name, include_private = false)
      return false unless name.to_s.ends_with?("?")
      return false unless attributes.any? { |a| a.respond_to?(name) }
    end

    private

    def attributes
      [rank, suit]
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fiftytwo-0.0.5 lib/fiftytwo/card.rb
fiftytwo-0.0.4 lib/fiftytwo/card.rb
fiftytwo-0.0.3 lib/fiftytwo/card.rb
fiftytwo-0.0.2 lib/fiftytwo/card.rb
fiftytwo-0.0.1 lib/fiftytwo/card.rb