Class: Basketball::Season::Team

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

Overview

Base class describing a team. A team here is bare metal and is just described by an ID and a collection of Player objects.

Defined Under Namespace

Classes: MaxPlayerCountError, PlayerNotSignedError

Constant Summary collapse

MAX_PLAYER_COUNT =
18

Instance Attribute Summary collapse

Attributes inherited from Entity

#id

Instance Method Summary collapse

Methods inherited from Entity

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

Constructor Details

#initialize(id:, name: '', players: []) ⇒ Team

Returns a new instance of Team.



15
16
17
18
19
20
21
22
23
24
# File 'lib/basketball/season/team.rb', line 15

def initialize(id:, name: '', players: [])
  super(id)

  @players = []
  @name    = name.to_s

  players.each { |p| sign!(p) }

  freeze
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/basketball/season/team.rb', line 13

def name
  @name
end

#playersObject (readonly)

Returns the value of attribute players.



13
14
15
# File 'lib/basketball/season/team.rb', line 13

def players
  @players
end

Instance Method Details

#release!(player) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
# File 'lib/basketball/season/team.rb', line 34

def release!(player)
  raise ArgumentError, 'player is required' unless player
  raise PlayerNotSignedError, "#{player} id not signed by #{self}" unless signed?(player)

  # Returns deleted player
  players.delete(player)
end

#sign!(player) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
# File 'lib/basketball/season/team.rb', line 42

def sign!(player)
  raise ArgumentError, 'player is required' unless player
  raise PlayerAlreadySignedError, "#{player} already signed by #{self}" if signed?(player)
  raise MaxPlayerCountError, "max reached: #{MAX_PLAYER_COUNT}" if players.length >= MAX_PLAYER_COUNT

  players << player

  self
end

#signed?(player) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/basketball/season/team.rb', line 30

def signed?(player)
  players.include?(player)
end

#to_sObject



26
27
28
# File 'lib/basketball/season/team.rb', line 26

def to_s
  (["[#{super}] #{name}"] + players.map(&:to_s)).join("\n")
end