Class: Rubygoal::Formation

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygoal/formation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFormation

Returns a new instance of Formation.



5
6
7
8
9
10
11
12
13
# File 'lib/rubygoal/formation.rb', line 5

def initialize
  @lineup = [
    [:none, :none, :none, :none, :none],
    [:none, :none, :none, :none, :none],
    [:none, :none, :none, :none, :none],
    [:none, :none, :none, :none, :none],
    [:none, :none, :none, :none, :none],
  ]
end

Instance Attribute Details

#lineupObject

Returns the value of attribute lineup.



3
4
5
# File 'lib/rubygoal/formation.rb', line 3

def lineup
  @lineup
end

Instance Method Details

#assign_column(index, column) ⇒ Object



43
44
45
46
47
# File 'lib/rubygoal/formation.rb', line 43

def assign_column(index, column)
  column.each_with_index do |player, row_index|
    lineup[row_index][index] = player
  end
end

#attackersObject



23
24
25
# File 'lib/rubygoal/formation.rb', line 23

def attackers
  column(4)
end

#attackers=(f) ⇒ Object



39
40
41
# File 'lib/rubygoal/formation.rb', line 39

def attackers=(f)
  assign_column(4, f)
end

#column(i) ⇒ Object



27
28
29
# File 'lib/rubygoal/formation.rb', line 27

def column(i)
  lineup.map { |row| row[i] }
end

#defendersObject



15
16
17
# File 'lib/rubygoal/formation.rb', line 15

def defenders
  column(0)
end

#defenders=(f) ⇒ Object



31
32
33
# File 'lib/rubygoal/formation.rb', line 31

def defenders=(f)
  assign_column(0, f)
end

#errorsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubygoal/formation.rb', line 49

def errors
  errors = {}

  unified  = lineup.flatten
  captains = unified.count(:captain)
  fast     = unified.count(:fast)
  average  = unified.count(:average)
  total    = captains + fast + average

  errors[:captains] = "La cantidad de capitanes es #{captains}" if captains != 1
  errors[:fast] = "La cantidad de rapidos es #{fast}" if fast != 3
  errors[:total] = "La cantidad total de jugadores es #{total}" if total != 10

  errors
end

#midfieldersObject



19
20
21
# File 'lib/rubygoal/formation.rb', line 19

def midfielders
  column(2)
end

#midfielders=(f) ⇒ Object



35
36
37
# File 'lib/rubygoal/formation.rb', line 35

def midfielders=(f)
  assign_column(2, f)
end

#valid?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/rubygoal/formation.rb', line 65

def valid?
  errors.empty?
end