Class: Rubygoal::Formation

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

Defined Under Namespace

Classes: CustomLines, CustomPosition, FormationDSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFormation

Returns a new instance of Formation.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rubygoal/formation.rb', line 7

def initialize
  @players_position = {}

  @lines_definition = {
    defenders: Field::WIDTH / 6,
    def_midfielders: Field::WIDTH / 3,
    midfielders: Field::WIDTH / 2,
    att_midfielders: Field::WIDTH / 3 * 2,
    attackers: Field::WIDTH / 6 * 5,
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



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

def method_missing(method, *args)
  line_name = method.to_s.chomp('=').to_sym
  if lines_definition[line_name]
    set_players_in_custom_line(lines_definition[line_name], args)
  end
end

Instance Attribute Details

#lines_definitionObject

Returns the value of attribute lines_definition.



5
6
7
# File 'lib/rubygoal/formation.rb', line 5

def lines_definition
  @lines_definition
end

#players_positionObject

Returns the value of attribute players_position.



5
6
7
# File 'lib/rubygoal/formation.rb', line 5

def players_position
  @players_position
end

Instance Method Details

#errorsObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/rubygoal/formation.rb', line 30

def errors
  errors = []

  # TODO Check if we need to count for the goalkeeper as well
  if players_position.size != 10
    errors << 'Incorrect number of players, are you missing a name?'
  end

  errors
end

#lineup(&block) ⇒ Object



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

def lineup(&block)
  instance_eval(&block)
end

#valid?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/rubygoal/formation.rb', line 41

def valid?
  errors.empty?
end