Class: ICU::Team

Inherits:
Object
  • Object
show all
Defined in:
lib/icu_tournament/team.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Team

Constructor. Name must be supplied.



45
46
47
48
# File 'lib/icu_tournament/team.rb', line 45

def initialize(name)
  self.name = name
  @members = Array.new
end

Instance Attribute Details

#membersObject (readonly)

Returns the value of attribute members.



42
43
44
# File 'lib/icu_tournament/team.rb', line 42

def members
  @members
end

#nameObject

Returns the value of attribute name.



42
43
44
# File 'lib/icu_tournament/team.rb', line 42

def name
  @name
end

Instance Method Details

#add_member(number) ⇒ Object

Add a team member referenced by any integer.



57
58
59
60
61
62
# 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

#include?(number) ⇒ Boolean

Detect if a member exists in a team.

Returns:

  • (Boolean)


74
75
76
# File 'lib/icu_tournament/team.rb', line 74

def include?(number)
  @members.include?(number)
end

#matches(name) ⇒ Object

Does the team name match the given string (ignoring case and spurious whitespace).



79
80
81
# File 'lib/icu_tournament/team.rb', line 79

def matches(name)
  self.name.downcase == name.strip.squeeze(' ').downcase
end

#renumber(map) ⇒ Object

Renumber the players according to the supplied hash. Return self.



65
66
67
68
69
70
71
# 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