Class: Yahtzee::ScoreCard

Inherits:
Struct
  • Object
show all
Defined in:
lib/score_card.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ ScoreCard

Returns a new instance of ScoreCard.



18
19
20
# File 'lib/score_card.rb', line 18

def initialize(attrs={})
  super *attrs.values_at(*self.class.members)
end

Class Method Details

.lower_keysObject



32
33
34
35
36
37
# File 'lib/score_card.rb', line 32

def self.lower_keys
  [:yahtzee, :bonus_yahtzee_1, :bonus_yahtzee_2, 
   :bonus_yahtzee_3, :three_of_a_kind,
   :four_of_a_kind, :full_house, :small_straight,
   :large_straight, :chance]
end

.meta_keysObject



52
53
54
55
56
57
# File 'lib/score_card.rb', line 52

def self.meta_keys
  [:bonus_yahtzee_1, :bonus_yahtzee_2,
   :bonus_yahtzee_3, :upper_subtotal,
   :upper_total,     :lower_subtotal,
   :game_total] 
end

.persist(score_card) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/score_card.rb', line 40

def self.persist(score_card)
  ->(placement, value) {
    old_values = score_card.to_hash
    new_values = { placement => value }
    score_card = new(old_values.merge(new_values))
  }
end

.placement_keysObject



48
49
50
# File 'lib/score_card.rb', line 48

def self.placement_keys
  Yahtzee::SCORINGS - meta_keys
end

.upper_keysObject



28
29
30
# File 'lib/score_card.rb', line 28

def self.upper_keys
  [:aces, :twos, :threes, :fours, :fives, :sixes]
end

Instance Method Details

#to_hashObject



22
23
24
25
26
# File 'lib/score_card.rb', line 22

def to_hash
  Yahtzee::SCORINGS.reduce({}) do |hash, attr|
    hash.merge(attr => send(attr))
  end
end