Class: SportsManager::Match

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

Overview

Public: A category’s match with teams, which rounds is ocurring and which matches its depends on before happening.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category:, id: nil, team1: nil, team2: nil, round: 0, depends_on: []) ⇒ Match

rubocop:disable Metrics/ParameterLists



20
21
22
23
24
25
26
27
28
# File 'lib/sports_manager/match.rb', line 20

def initialize(category:, id: nil, team1: nil, team2: nil, round: 0, depends_on: []) # rubocop:disable Metrics/ParameterLists
  @id = id
  @category = category
  @team1 = team1
  @team2 = team2
  @round = round
  @teams = [team1, team2].compact
  @depends_on = depends_on
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



7
8
9
# File 'lib/sports_manager/match.rb', line 7

def category
  @category
end

#depends_onObject (readonly)

Returns the value of attribute depends_on.



7
8
9
# File 'lib/sports_manager/match.rb', line 7

def depends_on
  @depends_on
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/sports_manager/match.rb', line 7

def id
  @id
end

#roundObject (readonly)

Returns the value of attribute round.



7
8
9
# File 'lib/sports_manager/match.rb', line 7

def round
  @round
end

#team1Object (readonly)

Returns the value of attribute team1.



7
8
9
# File 'lib/sports_manager/match.rb', line 7

def team1
  @team1
end

#team2Object (readonly)

Returns the value of attribute team2.



7
8
9
# File 'lib/sports_manager/match.rb', line 7

def team2
  @team2
end

#teamsObject (readonly)

Returns the value of attribute teams.



7
8
9
# File 'lib/sports_manager/match.rb', line 7

def teams
  @teams
end

Class Method Details

.build_next_match(category:, depends_on:, id: nil, round: 0) ⇒ Object



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

def self.build_next_match(category:, depends_on:, id: nil, round: 0)
  new(
    id: id,
    category: category,
    round: round,
    depends_on: depends_on,
    team1: NilTeam.new(category: category),
    team2: NilTeam.new(category: category)
  )
end

Instance Method Details

#==(other) ⇒ Object



73
74
75
76
77
# File 'lib/sports_manager/match.rb', line 73

def ==(other)
  return false unless instance_of?(other.class)

  id == other.id && category == other.category && round == other.round
end

#dependenciesObject



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

def dependencies
  @dependencies ||= depends_on
    .flat_map { |match| [match, *match.depends_on] }
end

#dependencies?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/sports_manager/match.rb', line 38

def dependencies?
  depends_on && !depends_on.empty?
end

#participantsObject



34
35
36
# File 'lib/sports_manager/match.rb', line 34

def participants
  @participants ||= teams.map(&:participants).flatten
end

#playable?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/sports_manager/match.rb', line 30

def playable?
  true
end

#playable_dependenciesObject



47
48
49
# File 'lib/sports_manager/match.rb', line 47

def playable_dependencies
  depends_on.select(&:playable?)
end

#previous_matchesObject



55
56
57
# File 'lib/sports_manager/match.rb', line 55

def previous_matches
  playable_dependencies.flat_map { |match| [match, *match.previous_matches] }
end

#previous_matches?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/sports_manager/match.rb', line 51

def previous_matches?
  previous_matches && !previous_matches.empty?
end

#teams_namesObject



69
70
71
# File 'lib/sports_manager/match.rb', line 69

def teams_names
  teams.map(&:name)
end

#title(title_format: 'M%<id>s') ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/sports_manager/match.rb', line 59

def title(title_format: 'M%<id>s')
  match_participants = if previous_matches?
    depends_on_names(title_format)
  else
    teams_names
  end

  match_participants.join(' vs. ')
end