class Rubygoal::Coach

Attributes

coach_definition[R]

Public Class Methods

new(coach_definition) click to toggle source
# File lib/rubygoal/coach.rb, line 9
def initialize(coach_definition)
  @coach_definition = coach_definition
end

Public Instance Methods

average_players() click to toggle source
# File lib/rubygoal/coach.rb, line 37
def average_players
  players_by_type(:average)
end
captain_player() click to toggle source
# File lib/rubygoal/coach.rb, line 29
def captain_player
  players_by_type(:captain).first
end
errors() click to toggle source
# File lib/rubygoal/coach.rb, line 13
def errors
  [].tap do |errors|
    check_unique_captain(errors)
    check_players_count(:average, errors)
    check_players_count(:fast, errors)
  end
end
fast_players() click to toggle source
# File lib/rubygoal/coach.rb, line 33
def fast_players
  players_by_type(:fast)
end
initial_formation() click to toggle source
# File lib/rubygoal/coach.rb, line 41
def initial_formation
  average_names = average_players.map(&:name)
  fast_names    = fast_players.map(&:name)
  captain_name  = captain_player.name

  formation = Formation.new

  formation.lineup do
    defenders average_names[0], average_names[2], :none, average_names[3], average_names[4]
    midfielders average_names[1], fast_names[0], :none, fast_names[1], average_names[5]
    attackers :none, captain_name, :none, fast_names[2], :none
  end

  formation
end
players_by_type(type) click to toggle source
# File lib/rubygoal/coach.rb, line 25
def players_by_type(type)
  players.select { |p| p.type == type }
end
valid?() click to toggle source
# File lib/rubygoal/coach.rb, line 21
def valid?
  errors.empty?
end

Private Instance Methods

check_players_count(type, errors) click to toggle source
# File lib/rubygoal/coach.rb, line 73
def check_players_count(type, errors)
  players_count = players_by_type(type).size

  if players_count != game_config.send("#{type}_players_count")
    errors << "The number of #{type} players is #{players_count}"
  end
end
check_unique_captain(errors) click to toggle source
# File lib/rubygoal/coach.rb, line 65
def check_unique_captain(errors)
  captain_count = players_by_type(:captain).size

  if captain_count != 1
    errors << "The number of captains is #{captain_count}"
  end
end
game_config() click to toggle source
# File lib/rubygoal/coach.rb, line 61
def game_config
  Rubygoal.configuration
end