Sha256: 403e3138d868913b7f1fbe27cd02ac87ebf99888a714932860910e366d1b9ea9

Contents?: true

Size: 882 Bytes

Versions: 3

Compression:

Stored size: 882 Bytes

Contents

module CardsLib
  class Ranker
    include Comparable
    attr :rank, :rank_lookup
    # Initialize's arguments:
    # rank        - is whatever part of the object, or the object itself you choose to use.
    # ranks       - is a simple hash to lookup a cards value with the keys matching the rank object.
    # rank_lookup - is an optional Proc to redefine how you will lookup a cards value.
    def initialize(rank = nil, ranks = nil, rank_lookup = nil)
      @rank, @ranks, @rank_lookup = rank, ranks, rank_lookup
    end

    def ranks
      @ranks || Standard::RANKS
    end

    def ranker(rank_face = @rank)
      @rank_lookup ? @rank_lookup.(rank_face) :
      ranks.index(rank_face).to_i + 1
    end

    def <=>(item)
      ranker(self.rank) <=> ranker(item.rank)
    end

    def sequential?(item)
      (ranker(self.rank) - ranker(item.rank)).abs == 1
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cards_lib-0.2.4 lib/cards_lib/ranker.rb
cards_lib-0.2.3 lib/cards_lib/ranker.rb
cards_lib-0.2.2 lib/cards_lib/ranker.rb