Class: Basketball::Season::Detail

Inherits:
ValueObject show all
Defined in:
lib/basketball/season/detail.rb

Overview

Describes a result from the perspective of a team.

Instance Method Summary collapse

Methods inherited from ValueObject

#<=>, #==, #[], #all_sorted_values, #hash, #to_h

Methods included from ValueObjectDSL

#all_sorted_value_keys, #all_value_keys, #value_keys, #value_reader

Constructor Details

#initialize(date:, home:, opponent:, opponent_score:, score:, opponent_type:) ⇒ Detail

rubocop:disable Metrics/CyclomaticComplexity

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/basketball/season/detail.rb', line 17

def initialize(date:, home:, opponent:, opponent_score:, score:, opponent_type:)
  super()

  raise ArgumentError,  'date is required'           unless date
  raise ArgumentError,  'opponent is required'       unless opponent
  raise ArgumentError,  'score is required'          unless score
  raise ArgumentError,  'opponent_score is required' unless opponent_score
  raise ArgumentError,  'home is required'           if home.nil?
  raise CannotTieError, 'scores cannot be equal'     if score == opponent_score
  raise ArgumentError,  'opponent_type is required'  unless opponent_type

  @date           = date
  @opponent       = opponent
  @score          = score
  @opponent_score = opponent_score
  @home           = home
  @opponent_type  = OpponentType.parse(opponent_type)

  freeze
end

Instance Method Details

#divisional?Boolean

rubocop:enable Metrics/CyclomaticComplexity

Returns:

  • (Boolean)


39
40
41
# File 'lib/basketball/season/detail.rb', line 39

def divisional?
  opponent_type == INTRA_DIVISIONAL
end

#inter_conterence?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/basketball/season/detail.rb', line 47

def inter_conterence?
  opponent_type == INTER_CONFERENCE
end

#intra_conference?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/basketball/season/detail.rb', line 43

def intra_conference?
  opponent_type == INTRA_CONFERENCE
end

#loss?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/basketball/season/detail.rb', line 55

def loss?
  score < opponent_score
end

#to_sObject



59
60
61
62
63
64
# File 'lib/basketball/season/detail.rb', line 59

def to_s
  win_display = win? ? 'win' : 'loss'
  vs_display  = home? ? 'vs' : 'at'

  "[#{date}] #{opponent_type} #{win_display} #{vs_display} #{opponent} (#{score}-#{opponent_score})"
end

#win?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/basketball/season/detail.rb', line 51

def win?
  score > opponent_score
end