Class: Basketball::Season::Team
- 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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
Attributes inherited from Entity
Instance Method Summary collapse
-
#initialize(id:, name: '', players: []) ⇒ Team
constructor
A new instance of Team.
- #release!(player) ⇒ Object
- #sign!(player) ⇒ Object
- #signed?(player) ⇒ Boolean
- #to_s ⇒ Object
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/basketball/season/team.rb', line 13 def name @name end |
#players ⇒ Object (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
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
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
30 31 32 |
# File 'lib/basketball/season/team.rb', line 30 def signed?(player) players.include?(player) end |
#to_s ⇒ Object
26 27 28 |
# File 'lib/basketball/season/team.rb', line 26 def to_s (["[#{super}] #{name}"] + players.map(&:to_s)).join("\n") end |