sportdb 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -53,7 +53,7 @@ class Runner
53
53
  cmd.on_tail( "-h", "--help", "Show this message" ) do
54
54
  puts <<EOS
55
55
 
56
- sportdb - sport.db command line tool
56
+ sportdb - sport.db command line tool, version #{VERSION}
57
57
 
58
58
  #{cmd.help}
59
59
 
@@ -1,6 +1,4 @@
1
- module SportDB
2
- module Models
3
-
1
+ module SportDB::Models
4
2
 
5
3
  class Event < ActiveRecord::Base
6
4
 
@@ -11,9 +9,13 @@ class Event < ActiveRecord::Base
11
9
  has_many :event_teams, :class_name => 'EventTeam'
12
10
  has_many :teams, :through => :event_teams
13
11
 
14
- end # class Event
12
+ def add_teams_from_ary!( team_keys )
13
+ team_keys.each do |team_key|
14
+ team = Team.find_by_key!( team_key )
15
+ self.teams << team
16
+ end
17
+ end
15
18
 
19
+ end # class Event
16
20
 
17
-
18
- end # module Models
19
- end # module SportDB
21
+ end # module SportDB::Models
@@ -1,5 +1,4 @@
1
- module SportDB
2
- module Models
1
+ module SportDB::Models
3
2
 
4
3
 
5
4
  class Group < ActiveRecord::Base
@@ -10,9 +9,15 @@ class Group < ActiveRecord::Base
10
9
  has_many :group_teams, :class_name => 'GroupTeam'
11
10
  has_many :teams, :through => :group_teams
12
11
 
12
+ def add_teams_from_ary!( team_keys )
13
+ team_keys.each do |team_key|
14
+ team = Team.find_by_key!( team_key )
15
+ self.teams << team
16
+ end
17
+ end
18
+
13
19
  end # class Group
14
20
 
15
21
 
16
- end # module Models
17
- end # module SportDB
22
+ end # module SportDB::Models
18
23
 
@@ -72,10 +72,22 @@ class Reader
72
72
 
73
73
 
74
74
  def is_round?( line )
75
- line =~ /Spieltag|Runde/
75
+ line =~ /Spieltag|Runde|Achtelfinale|Viertelfinale|Halbfinale|Finale/
76
+ end
77
+
78
+ def find_knockout_flag( line )
79
+ if line =~ /Achtelfinale|Viertelfinale|Halbfinale|Finale|K\.O\.|Knockout/
80
+ puts " setting knockout flag to true"
81
+ true
82
+ else
83
+ false
84
+ end
76
85
  end
77
86
 
78
87
  def find_round_pos!( line )
88
+ ## fix/todo:
89
+ ## if no round found assume last_pos+1 ??? why? why not?
90
+
79
91
  regex = /\b(\d+)\b/
80
92
 
81
93
  if line =~ regex
@@ -126,7 +138,26 @@ class Reader
126
138
  end
127
139
  end
128
140
 
129
- def find_score!( line )
141
+
142
+ def find_game_pos!( line )
143
+ # extract optional game pos from line
144
+ # and return it
145
+ # NB: side effect - removes pos from line string
146
+
147
+ # e.g. (1) - must start line
148
+ regex = /^[ \t]*\((\d{1,3})\)[ \t]+/
149
+ if line =~ regex
150
+ puts " pos: >#{$1}<"
151
+
152
+ line.sub!( regex, '[POS] ' )
153
+ return $1.to_i
154
+ else
155
+ return nil
156
+ end
157
+
158
+ end
159
+
160
+ def find_scores!( line )
130
161
  # extract score from line
131
162
  # and return it
132
163
  # NB: side effect - removes date from line string
@@ -134,17 +165,42 @@ class Reader
134
165
  # e.g. 1:2 or 0:2 or 3:3
135
166
  regex = /\b(\d):(\d)\b/
136
167
 
168
+ # e.g. 1:2nV => overtime
169
+ regex_ot = /\b(\d):(\d)[ \t]?[nN][vV]\b/
170
+
171
+ # e.g. 5:4iE => penalty
172
+ regex_p = /\b(\d):(\d)[ \t]?[iI][eE]\b/
173
+
174
+ scores = []
175
+
137
176
  if line =~ regex
138
- value = "#{$1}-#{$2}"
139
- puts " score: >#{value}<"
177
+ puts " score: >#{$1}-#{$2}<"
140
178
 
141
179
  line.sub!( regex, '[SCORE]' )
142
180
 
143
- return [$1.to_i,$2.to_i]
144
- else
145
- return []
181
+ scores << $1.to_i
182
+ scores << $2.to_i
183
+
184
+ if line =~ regex_ot
185
+ puts " score.ot: >#{$1}-#{$2}<"
186
+
187
+ line.sub!( regex_ot, '[SCORE.OT]' )
188
+
189
+ scores << $1.to_i
190
+ scores << $2.to_i
191
+
192
+ if line =~ regex_p
193
+ puts " score.p: >#{$1}-#{$2}<"
194
+
195
+ line.sub!( regex_p, '[SCORE.P]' )
196
+
197
+ scores << $1.to_i
198
+ scores << $2.to_i
199
+ end
200
+ end
146
201
  end
147
- end
202
+ scores
203
+ end # methdod find_scores!
148
204
 
149
205
 
150
206
  def find_team_worker!( line, index )
@@ -221,6 +277,8 @@ class Reader
221
277
  if is_round?( line )
222
278
  puts "parsing round line: >#{line}<"
223
279
  pos = find_round_pos!( line )
280
+
281
+ @knockout_flag = find_knockout_flag( line )
224
282
  puts " line: >#{line}<"
225
283
 
226
284
  ## NB: dummy/placeholder start_at, end_at date
@@ -257,12 +315,14 @@ class Reader
257
315
  else
258
316
  puts "parsing game (fixture) line: >#{line}<"
259
317
 
318
+ pos = find_game_pos!( line )
319
+
260
320
  match_teams!( line )
261
321
  team1_key = find_team1!( line )
262
322
  team2_key = find_team2!( line )
263
323
 
264
324
  date = find_date!( line )
265
- score = find_score!( line )
325
+ scores = find_scores!( line )
266
326
 
267
327
  puts " line: >#{line}<"
268
328
 
@@ -279,10 +339,17 @@ class Reader
279
339
  )
280
340
 
281
341
  game_attribs = {
282
- score1: score[0],
283
- score2: score[1],
284
- play_at: date
342
+ score1: scores[0],
343
+ score2: scores[1],
344
+ score3: scores[2],
345
+ score4: scores[3],
346
+ score5: scores[4],
347
+ score6: scores[5],
348
+ play_at: date,
349
+ knockout: @knockout_flag
285
350
  }
351
+
352
+ game_attribs[ :pos ] = pos if pos.present?
286
353
 
287
354
  if game.present?
288
355
  puts "*** update game #{game.id}:"
@@ -291,13 +358,15 @@ class Reader
291
358
  game = Game.new
292
359
 
293
360
  more_game_attribs = {
294
- ## NB: use round.games.count for pos
295
- ## lets us add games out of order if later needed
296
- pos: @round.games.count+1,
297
361
  round_id: @round.id,
298
362
  team1_id: team1.id,
299
363
  team2_id: team2.id
300
- }
364
+ }
365
+
366
+ ## NB: use round.games.count for pos
367
+ ## lets us add games out of order if later needed
368
+ more_game_attribs[ :pos ] = @round.games.count+1 if pos.nil?
369
+
301
370
  game_attribs = game_attribs.merge( more_game_attribs )
302
371
  end
303
372
 
@@ -313,6 +382,11 @@ class Reader
313
382
  round = Round.find( k )
314
383
  games = round.games.order( 'play_at asc' ).all
315
384
 
385
+ ## skip rouns w/ no games
386
+
387
+ ## todo/fix: what's the best way for checking assoc w/ 0 recs?
388
+ next if games.size == 0
389
+
316
390
  round_attribs = {}
317
391
 
318
392
  ## todo: check for no records
@@ -1,4 +1,4 @@
1
1
 
2
2
  module SportDB
3
- VERSION = '0.4.2'
3
+ VERSION = '0.4.3'
4
4
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sportdb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 2
10
- version: 0.4.2
9
+ - 3
10
+ version: 0.4.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gerald Bauer
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-10-27 00:00:00 Z
18
+ date: 2012-10-28 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activerecord