Class: Rubygoal::Coach

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rubygoal/coach.rb

Instance Method Summary collapse

Constructor Details

#initialize(coach_definition) ⇒ Coach

Returns a new instance of Coach.



9
10
11
# File 'lib/rubygoal/coach.rb', line 9

def initialize(coach_definition)
  @coach_definition = coach_definition
end

Instance Method Details

#average_playersObject



37
38
39
# File 'lib/rubygoal/coach.rb', line 37

def average_players
  players_by_type(:average)
end

#captain_playerObject



29
30
31
# File 'lib/rubygoal/coach.rb', line 29

def captain_player
  players_by_type(:captain).first
end

#errorsObject



13
14
15
16
17
18
19
# 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_playersObject



33
34
35
# File 'lib/rubygoal/coach.rb', line 33

def fast_players
  players_by_type(:fast)
end

#initial_formationObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



25
26
27
# File 'lib/rubygoal/coach.rb', line 25

def players_by_type(type)
  players.select { |p| p.type == type }
end

#valid?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rubygoal/coach.rb', line 21

def valid?
  errors.empty?
end