Class: SportsManager::Team

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

Overview

Public: Participants in a team for a tournament category

Direct Known Subclasses

DoubleTeam, SingleTeam

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(participants:, category: nil) ⇒ Team

Returns a new instance of Team.



20
21
22
23
# File 'lib/sports_manager/team.rb', line 20

def initialize(participants:, category: nil)
  @category = category
  @participants = participants
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



6
7
8
# File 'lib/sports_manager/team.rb', line 6

def category
  @category
end

#participantsObject (readonly)

Returns the value of attribute participants.



6
7
8
# File 'lib/sports_manager/team.rb', line 6

def participants
  @participants
end

Class Method Details

.for(participants:, category:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sports_manager/team.rb', line 8

def self.for(participants:, category:)
  klass = case participants.size
          when 1 then SingleTeam
          when 2 then DoubleTeam
          else raise StandardError,
                     "Participants #{participants} is not " \
                     'between 1 and 2'
  end

  klass.new(participants: participants, category: category)
end

Instance Method Details

#==(other) ⇒ Object



41
42
43
44
45
# File 'lib/sports_manager/team.rb', line 41

def ==(other)
  instance_of?(other.class) &&
    category == other.category &&
    participants == other.participants
end

#find_participant(id) ⇒ Object



31
32
33
# File 'lib/sports_manager/team.rb', line 31

def find_participant(id)
  participants.find { |participant| participant.id == id }
end

#find_participants(ids) ⇒ Object



35
36
37
38
39
# File 'lib/sports_manager/team.rb', line 35

def find_participants(ids)
  unique_ids = ids.uniq
  found_participants = unique_ids.map(&method(:find_participant)).compact
  found_participants if found_participants.size == unique_ids.size
end

#nameObject



25
26
27
28
29
# File 'lib/sports_manager/team.rb', line 25

def name
  participants
    .flat_map(&:name)
    .join(' e ')
end