Move self methods into self block

This commit is contained in:
Daniel Schädler 2019-05-10 11:34:01 +02:00
parent 6f4c7d3edd
commit a4ea0cf979
1 changed files with 26 additions and 24 deletions

View File

@ -1,32 +1,34 @@
# frozen_string_literal: true
class GroupStageService
def self.generate_group_stage(groups)
raise 'Cannot generate group stage without groups' if groups.length.zero?
class << self
def generate_group_stage(groups)
raise 'Cannot generate group stage without groups' if groups.length.zero?
# raise an error if the average group size is not a whole number
raise 'Groups need to be equal size' unless (groups.flatten.length.to_f / groups.length.to_f % 1).zero?
# raise an error if the average group size is not a whole number
raise 'Groups need to be equal size' unless (groups.flatten.length.to_f / groups.length.to_f % 1).zero?
groups = groups.map(&method(:get_group_object_from))
Stage.new level: -1, groups: groups
end
def self.get_group_object_from(team_array)
Group.new matches: generate_all_matches_between(team_array)
end
def self.generate_all_matches_between(teams)
matches = []
teams.combination(2).to_a # = matchups
.each_with_index do |matchup, i|
match = Match.new state: :not_started,
position: i,
match_scores: [
MatchScore.new(team: matchup.first),
MatchScore.new(team: matchup.second)
]
matches << match
groups = groups.map(&method(:get_group_object_from))
Stage.new level: -1, groups: groups
end
def get_group_object_from(team_array)
Group.new matches: generate_all_matches_between(team_array)
end
def generate_all_matches_between(teams)
matches = []
teams.combination(2).to_a # = matchups
.each_with_index do |matchup, i|
match = Match.new state: :not_started,
position: i,
match_scores: [
MatchScore.new(team: matchup.first),
MatchScore.new(team: matchup.second)
]
matches << match
end
matches
end
matches
end
end