class ICU::Team
A team consists of a name and one or more players referenced by numbers. Typically the team will be attached to a tournament (ICU::Tournament
) and the numbers will the unique numbers by which the players in that tournament are referenced. To instantiate a team, you must supply a name.
team = ICU::Team.new('Wandering Dragons')
Then you simply add player’s (numbers) to it.
team.add_player(1) team.add_payeer(3) team.add_player(7)
To get the current members of a team
team.members # => [1, 3, 7]
You can enquire whether a team contains a given player number.
team.contains?(3) # => true team.contains?(4) # => false
Or whether it matches a given name (which ignoring case and removing spurious whitespace)
team.matches(' wandering dragons ') # => true team.matches('Blundering Bishops') # => false
Whenever you reset the name of a tournament spurious whitespace is removed but case is not altered.
team.name = ' blundering bishops ' team.name # => "blundering bishops"
Attempting to add non-numbers or duplicate numbers as new team members results in an exception.
team.add(nil) # exception - not a number team.add(3) # exception - already a member
Attributes
Public Class Methods
Constructor. Name must be supplied.
# File lib/icu_tournament/team.rb, line 45 def initialize(name) self.name = name @members = Array.new end
Public Instance Methods
Add a team member referenced by any integer.
# File lib/icu_tournament/team.rb, line 57 def add_member(number) pnum = number.to_i raise "'#{number}' is not a valid as a team member player number" if pnum == 0 && !number.to_s.match(/^[^\d]*0/) raise "can't add duplicate player number #{pnum} to team '#{@name}'" if @members.include?(pnum) @members.push(pnum) end
Detect if a member exists in a team.
# File lib/icu_tournament/team.rb, line 74 def include?(number) @members.include?(number) end
Does the team name match the given string (ignoring case and spurious whitespace).
# File lib/icu_tournament/team.rb, line 79 def matches(name) self.name.downcase == name.strip.squeeze(' ').downcase end
Set name. Must not be blank.
# File lib/icu_tournament/team.rb, line 51 def name=(name) @name = name.strip.squeeze(' ') raise "team can't be blank" if @name.length == 0 end
Renumber the players according to the supplied hash. Return self.
# File lib/icu_tournament/team.rb, line 65 def renumber(map) @members.each_with_index do |pnum, index| raise "player number #{pnum} not found in renumbering hash" unless map[pnum] @members[index] = map[pnum] end self end