Class: Basketball::Season::Calendar

Inherits:
Entity
  • Object
show all
Defined in:
lib/basketball/season/calendar.rb

Overview

Sets boundaries for exhibition and regular season play. Add games as long as they are within the correct dated boundaries

Defined Under Namespace

Classes: OutOfBoundsError, TeamAlreadyBookedError

Instance Attribute Summary collapse

Attributes inherited from Entity

#id

Instance Method Summary collapse

Methods inherited from Entity

#<=>, #==, #comparable_id, #hash, #to_s

Constructor Details

#initialize(exhibition_start_date:, exhibition_end_date:, regular_start_date:, regular_end_date:, games: []) ⇒ Calendar

Returns a new instance of Calendar.

Raises:

  • (ArgumentError)


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

def initialize(
  exhibition_start_date:,
  exhibition_end_date:,
  regular_start_date:,
  regular_end_date:,
  games: []
)
  super()

  raise ArgumentError, 'exhibition_start_date is required' if exhibition_start_date.to_s.empty?
  raise ArgumentError, 'exhibition_end_date is required'   if exhibition_end_date.to_s.empty?
  raise ArgumentError, 'regular_start_date is required'    if regular_start_date.to_s.empty?
  raise ArgumentError, 'regular_end_date is required'      if regular_end_date.to_s.empty?

  @exhibition_start_date = exhibition_start_date
  @exhibition_end_date   = exhibition_end_date
  @regular_start_date    = regular_start_date
  @regular_end_date      = regular_end_date
  @games                 = []

  games.each { |game| add!(game) }
end

Instance Attribute Details

#exhibition_end_dateObject (readonly)

Returns the value of attribute exhibition_end_date.



11
12
13
# File 'lib/basketball/season/calendar.rb', line 11

def exhibition_end_date
  @exhibition_end_date
end

#exhibition_start_dateObject (readonly)

Returns the value of attribute exhibition_start_date.



11
12
13
# File 'lib/basketball/season/calendar.rb', line 11

def exhibition_start_date
  @exhibition_start_date
end

#gamesObject (readonly)

Returns the value of attribute games.



11
12
13
# File 'lib/basketball/season/calendar.rb', line 11

def games
  @games
end

#regular_end_dateObject (readonly)

Returns the value of attribute regular_end_date.



11
12
13
# File 'lib/basketball/season/calendar.rb', line 11

def regular_end_date
  @regular_end_date
end

#regular_start_dateObject (readonly)

Returns the value of attribute regular_start_date.



11
12
13
# File 'lib/basketball/season/calendar.rb', line 11

def regular_start_date
  @regular_start_date
end

Instance Method Details

#add!(game) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/basketball/season/calendar.rb', line 40

def add!(game)
  assert_in_bounds(game)
  assert_free_date(game)

  @games << game

  self
end

#available_exhibition_dates_for(opponent) ⇒ Object



63
64
65
# File 'lib/basketball/season/calendar.rb', line 63

def available_exhibition_dates_for(opponent)
  all_exhibition_dates - exhibitions_for(opponent:).map(&:date)
end

#available_exhibition_matchup_dates(opponent1, opponent2) ⇒ Object



71
72
73
74
75
76
# File 'lib/basketball/season/calendar.rb', line 71

def available_exhibition_matchup_dates(opponent1, opponent2)
  available_opponent_dates       = available_exhibition_dates_for(opponent1)
  available_other_opponent_dates = available_exhibition_dates_for(opponent2)

  available_opponent_dates & available_other_opponent_dates
end

#available_regular_dates_for(opponent) ⇒ Object



67
68
69
# File 'lib/basketball/season/calendar.rb', line 67

def available_regular_dates_for(opponent)
  all_season_dates - regulars_for(opponent:).map(&:date)
end

#available_regular_matchup_dates(opponent1, opponent2) ⇒ Object



78
79
80
81
82
83
# File 'lib/basketball/season/calendar.rb', line 78

def available_regular_matchup_dates(opponent1, opponent2)
  available_opponent_dates       = available_regular_dates_for(opponent1)
  available_other_opponent_dates = available_regular_dates_for(opponent2)

  available_opponent_dates & available_other_opponent_dates
end

#exhibitions_for(date: nil, opponent: nil) ⇒ Object



49
50
51
# File 'lib/basketball/season/calendar.rb', line 49

def exhibitions_for(date: nil, opponent: nil)
  games_for(date:, opponent:).select { |game| game.is_a?(Exhibition) }
end

#games_for(date: nil, opponent: nil) ⇒ Object



57
58
59
60
61
# File 'lib/basketball/season/calendar.rb', line 57

def games_for(date: nil, opponent: nil)
  games.select do |game|
    (date.nil? || game.date == date) && (opponent.nil? || game.for?(opponent))
  end
end

#regulars_for(date: nil, opponent: nil) ⇒ Object



53
54
55
# File 'lib/basketball/season/calendar.rb', line 53

def regulars_for(date: nil, opponent: nil)
  games_for(date:, opponent:).select { |game| game.is_a?(Regular) }
end