Class: Sports::Goal

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/csv/goal.rb,
lib/sportdb/structs/goal.rb

Overview

nested (non-freestanding) inside match (match is parent)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(team:, player:, minute:, offset: nil, owngoal: false, penalty: false, score1: nil, score2: nil, notes: nil) ⇒ Goal

note: make score1,score2 optional for now !!!!



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sportdb/structs/goal.rb', line 26

def initialize( team:,
                player:,
                minute:,
                offset:  nil,
                owngoal: false,
                penalty: false,
                score1:  nil,
                score2:  nil,
                notes:   nil
              )
  @score1  = score1
  @score2  = score2

  @team    = team     # 1 or 2
  @player  = player
  @minute  = minute
  @offset  = offset
  @owngoal = owngoal
  @penalty = penalty
  @notes   = notes
end

Instance Attribute Details

#minuteObject (readonly)

Returns the value of attribute minute.



5
6
7
# File 'lib/sportdb/structs/goal.rb', line 5

def minute
  @minute
end

#notesObject (readonly)

Returns the value of attribute notes.



5
6
7
# File 'lib/sportdb/structs/goal.rb', line 5

def notes
  @notes
end

#offsetObject (readonly)

Returns the value of attribute offset.



5
6
7
# File 'lib/sportdb/structs/goal.rb', line 5

def offset
  @offset
end

#owngoalObject (readonly)

Returns the value of attribute owngoal.



5
6
7
# File 'lib/sportdb/structs/goal.rb', line 5

def owngoal
  @owngoal
end

#penaltyObject (readonly)

Returns the value of attribute penalty.



5
6
7
# File 'lib/sportdb/structs/goal.rb', line 5

def penalty
  @penalty
end

#playerObject (readonly) Also known as: name

Returns the value of attribute player.



5
6
7
# File 'lib/sportdb/structs/goal.rb', line 5

def player
  @player
end

#score1Object (readonly)

Returns the value of attribute score1.



5
6
7
# File 'lib/sportdb/structs/goal.rb', line 5

def score1
  @score1
end

#score2Object (readonly)

Returns the value of attribute score2.



5
6
7
# File 'lib/sportdb/structs/goal.rb', line 5

def score2
  @score2
end

#teamObject (readonly)

Returns the value of attribute team.



5
6
7
# File 'lib/sportdb/structs/goal.rb', line 5

def team
  @team
end

Class Method Details

.build(events) ⇒ Object

nested (non-freestanding) inside match (match is parent)



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/sportdb/csv/goal.rb', line 146

def self.build( events )  ## check/todo - rename to build_from_event/row or such - why? why not?

  ## build an array of goal structs from (csv) recs

  recs = []

  last_score1 = 0
  last_score2 = 0

  events.each do |event|

    if last_score1+1 == event.score1 && last_score2 == event.score2
      team = 1
    elsif last_score2+1 == event.score2 && last_score1 == event.score1
      team = 2
    else
      puts "!! ERROR - unexpected score advance (one goal at a time expected):"
      puts "  #{last_score1}-#{last_score2}=> #{event.score1}-#{event.score2}"
      exit 1
    end

    last_score1 = event.score1
    last_score2 = event.score2


    attributes = {
      score1:  event.score1,
      score2:  event.score2,
      team:    team,
      minute:  event.minute,
      offset:  event.offset,
      player:  event.player,
      owngoal: event.owngoal,
      penalty: event.penalty,
      notes:   event.notes
    }

    recs << new( **attributes )
  end

  recs
end

Instance Method Details

#==(o) ⇒ Object



56
57
58
# File 'lib/sportdb/structs/goal.rb', line 56

def ==(o)
  o.class == self.class && o.state == state
end

#owngoal?Boolean

Returns:

  • (Boolean)


19
# File 'lib/sportdb/structs/goal.rb', line 19

def owngoal?() @owngoal==true; end

#penalty?Boolean

Returns:

  • (Boolean)


20
# File 'lib/sportdb/structs/goal.rb', line 20

def penalty?() @penalty==true; end

#pretty_print(printer) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sportdb/structs/goal.rb', line 60

def pretty_print( printer )
  buf = String.new
  buf << "<Goal: #{@score1 ? @score1 : '?'}-#{@score2 ? @score2 : '?'}"
  buf << " #{@player} #{@minute}"
  buf << "+#{@offset}"    if @offset && @offset > 0
  buf << "'"
  buf << " (o.g.)"  if @owngoal
  buf << " (pen.)"  if @penalty
  buf << " for #{@team}"     ### team 1 or 2 - use home/away
  buf << " -- #{@notes}"   if @notes
  buf << ">"

  printer.text( buf )
end

#stateObject



48
49
50
51
52
53
54
# File 'lib/sportdb/structs/goal.rb', line 48

def state
  [@score1, @score2,
   @team,
   @player, @minute, @offset, @owngoal, @penalty,
   @notes
   ]
end

#team1?Boolean

Returns:

  • (Boolean)


21
# File 'lib/sportdb/structs/goal.rb', line 21

def team1?()   @team == 1; end

#team2?Boolean

Returns:

  • (Boolean)


22
# File 'lib/sportdb/structs/goal.rb', line 22

def team2?()   @team == 2; end