lib/cards_lib/ranker.rb in cards_lib-0.1.2 vs lib/cards_lib/ranker.rb in cards_lib-0.2.0

- old
+ new

@@ -1,21 +1,26 @@ module CardsLib class Ranker include Comparable - attr :rank, :ranks, :rank_lookup - def initialize rank = nil, - ranks: "A23456789TJQK".chars, - rank_lookup: nil - - @rank, @ranks, @rank_lookup = rank, ranks, rank_lookup || - ->rank_face{ @ranks.index(rank_face).to_i + 1 } + attr :rank, :rank_lookup + 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) - @rank_lookup[self.rank] <=> @rank_lookup[item.rank] + ranker(self.rank) <=> ranker(item.rank) end def sequential?(item) - (@rank_lookup[self.rank] - @rank_lookup[item.rank]).abs == 1 + (ranker(self.rank) - ranker(item.rank)).abs == 1 end end end