Class: Sports::GoalEvent

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

Overview

“free-standing” goal event - for import/export in separate event / goal datafiles

returned by CsvGoalParser and others

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(team1:, team2:, round: nil, date: nil, score1:, score2:, player:, minute:, offset: nil, owngoal: false, penalty: false, notes: nil) ⇒ GoalEvent

Returns a new instance of GoalEvent.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/sportdb/quick/csv/goal.rb', line 105

def initialize( team1:,
                team2:,
                round:   nil,
                date:    nil,
                score1:,
                score2:,
                player:,
                minute:,
                offset:  nil,
                owngoal: false,
                penalty: false,
                notes:   nil
              )
  @team1   = team1
  @team2   = team2
  @round   = round
  @date    = date

  @score1  = score1
  @score2  = score2
  @player  = player
  @minute  = minute
  @offset  = offset
  @owngoal = owngoal
  @penalty = penalty
  @notes   = notes
end

Instance Attribute Details

#dateObject (readonly)

match id



76
77
78
# File 'lib/sportdb/quick/csv/goal.rb', line 76

def date
  @date
end

#minuteObject (readonly)

main attributes



82
83
84
# File 'lib/sportdb/quick/csv/goal.rb', line 82

def minute
  @minute
end

#notesObject (readonly)

main attributes



82
83
84
# File 'lib/sportdb/quick/csv/goal.rb', line 82

def notes
  @notes
end

#offsetObject (readonly)

main attributes



82
83
84
# File 'lib/sportdb/quick/csv/goal.rb', line 82

def offset
  @offset
end

#owngoalObject (readonly)

main attributes



82
83
84
# File 'lib/sportdb/quick/csv/goal.rb', line 82

def owngoal
  @owngoal
end

#penaltyObject (readonly)

main attributes



82
83
84
# File 'lib/sportdb/quick/csv/goal.rb', line 82

def penalty
  @penalty
end

#playerObject (readonly)

main attributes



82
83
84
# File 'lib/sportdb/quick/csv/goal.rb', line 82

def player
  @player
end

#roundObject (readonly)

match id



76
77
78
# File 'lib/sportdb/quick/csv/goal.rb', line 76

def round
  @round
end

#score1Object (readonly)

main attributes



82
83
84
# File 'lib/sportdb/quick/csv/goal.rb', line 82

def score1
  @score1
end

#score2Object (readonly)

main attributes



82
83
84
# File 'lib/sportdb/quick/csv/goal.rb', line 82

def score2
  @score2
end

#team1Object (readonly)

match id



76
77
78
# File 'lib/sportdb/quick/csv/goal.rb', line 76

def team1
  @team1
end

#team2Object (readonly)

match id



76
77
78
# File 'lib/sportdb/quick/csv/goal.rb', line 76

def team2
  @team2
end

Class Method Details

.build(row) ⇒ Object

rename to parse or such - why? why not?



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sportdb/quick/csv/goal.rb', line 8

def self.build( row )  ## rename to parse or such - why? why not?

## split match_id
team_str, more_str   = row['Match'].split( '|' )
team1_str, team2_str = team_str.split( ' - ' )

more_str  = more_str.strip
team1_str = team1_str.strip
team2_str = team2_str.strip

 # check if more_str is a date otherwise assume round
 date_fmt = if more_str =~ /^[A-Z]{3} [0-9]{1,2}$/i  ## Apr 4
              '%b %d'
            elsif more_str =~ /^[A-Z]{3} [0-9]{1,2} [0-9]{4}$/i  ## Apr 4 2019
              '%b %d %Y'
           else
              nil
           end

 if date_fmt
  date  = Date.strptime( more_str, date_fmt )
  round = nil
 else
  date  = nil
  round = more_str
 end


  values = row['Score'].split('-')
  values = values.map { |value| value.strip }
  score1 = values[0].to_i
  score2 = values[1].to_i

  minute = nil
  offset = nil
  if m=%r{([0-9]+)
            (?:[ ]+
                 \+([0-9]+)
              )?
              ['.]
        $}x.match( row['Minute'])
    minute = m[1].to_i
    offset = m[2] ? m[2].to_i : nil
  else
    puts "!! ERROR - unsupported minute (goal) format >#{row['Minute']}<"
    exit 1
  end

  attributes = {
        team1:  team1_str,
        team2:  team2_str,
        date:   date,
        round:  round,
        score1: score1,
        score2: score2,
        minute: minute,
        offset: offset,
        player: row['Player'],
        owngoal: ['(og)', '(o.g.)'].include?( row['Extra']),
        penalty: ['(pen)', '(pen.)'].include?( row['Extra']),
        notes:   (row['Notes'].nil? || row['Notes'].empty?) ? nil : row['Notes']
      }

  new( **attributes )
end

Instance Method Details

#match_idObject

todo/check: or just use match.hash or such if match mapping known - why? why not?



93
94
95
96
97
98
99
# File 'lib/sportdb/quick/csv/goal.rb', line 93

def match_id
  if round
    "#{@team1} - #{@team2} | #{@round}"
  else
    "#{@team1} - #{@team2} | #{@date}"
  end
end

#owngoal?Boolean

Returns:

  • (Boolean)


102
# File 'lib/sportdb/quick/csv/goal.rb', line 102

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

#penalty?Boolean

Returns:

  • (Boolean)


103
# File 'lib/sportdb/quick/csv/goal.rb', line 103

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

#update(**kwargs) ⇒ Object

note: lets you use normalize teams or such acts like a Match struct



135
136
137
138
139
# File 'lib/sportdb/quick/csv/goal.rb', line 135

def update( **kwargs )
  ## todo/fix: use team1_name, team2_name or similar - for compat with db activerecord version? why? why not?
  @team1 = kwargs[:team1]    if kwargs.has_key? :team1
  @team2 = kwargs[:team2]    if kwargs.has_key? :team2
end