Class: Basketball::Season::Standings

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

Overview

Represents a League with each team’s win/loss details.

Defined Under Namespace

Classes: TeamAlreadyRegisteredError, TeamNotRegisteredError

Instance Attribute Summary

Attributes inherited from Entity

#id

Instance Method Summary collapse

Methods inherited from Entity

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

Constructor Details

#initialize(records: []) ⇒ Standings



10
11
12
13
14
15
16
# File 'lib/basketball/season/standings.rb', line 10

def initialize(records: [])
  super()

  @records_by_id = {}

  records.each { |record| add!(record) }
end

Instance Method Details

#accept!(result) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/basketball/season/standings.rb', line 43

def accept!(result)
  [
    record_for(result.home_opponent),
    record_for(result.away_opponent)
  ].each do |record|
    record.accept!(result)
  end

  self
end

#add!(record) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
# File 'lib/basketball/season/standings.rb', line 18

def add!(record)
  raise ArgumentError, 'record is required' unless record
  raise TeamAlreadyRegisteredError, "#{team} already registered!" if team?(record)

  records_by_id[record.id] = record

  self
end

#record_for(team) ⇒ Object



37
38
39
40
41
# File 'lib/basketball/season/standings.rb', line 37

def record_for(team)
  raise TeamNotRegisteredError, "#{team} not registered" unless team?(team)

  records_by_id.fetch(team.id)
end

#recordsObject



33
34
35
# File 'lib/basketball/season/standings.rb', line 33

def records
  records_by_id.values
end

#register!(team) ⇒ Object



27
28
29
30
31
# File 'lib/basketball/season/standings.rb', line 27

def register!(team)
  add!(Record.new(id: team.id))

  self
end

#team?(team) ⇒ Boolean



54
55
56
# File 'lib/basketball/season/standings.rb', line 54

def team?(team)
  records_by_id.key?(team.id)
end

#to_sObject



58
59
60
# File 'lib/basketball/season/standings.rb', line 58

def to_s
  records.sort.reverse.map.with_index(1) { |r, i| "##{i} #{r}" }.join("\n")
end