sportdb-models 1.18.5 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. checksums.yaml +4 -4
  2. data/Manifest.txt +27 -145
  3. data/Rakefile +4 -5
  4. data/lib/sportdb/models.rb +93 -160
  5. data/lib/sportdb/{deleter.rb → models/deleter.rb} +4 -10
  6. data/lib/sportdb/models/formats.rb +23 -0
  7. data/lib/sportdb/models/models/assoc.rb +38 -0
  8. data/lib/sportdb/models/{badge.rb → models/badge.rb} +1 -1
  9. data/lib/sportdb/models/models/event.rb +55 -0
  10. data/lib/sportdb/models/{forward.rb → models/forward.rb} +3 -3
  11. data/lib/sportdb/models/{goal.rb → models/goal.rb} +1 -1
  12. data/lib/sportdb/models/models/ground.rb +16 -0
  13. data/lib/sportdb/models/{group.rb → models/group.rb} +10 -7
  14. data/lib/sportdb/models/models/league.rb +20 -0
  15. data/lib/sportdb/models/{roster.rb → models/lineup.rb} +3 -4
  16. data/lib/sportdb/models/{game.rb → models/match.rb} +18 -61
  17. data/lib/sportdb/models/{person.rb → models/person.rb} +0 -0
  18. data/lib/sportdb/models/{round.rb → models/round.rb} +1 -6
  19. data/lib/sportdb/models/{season.rb → models/season.rb} +2 -1
  20. data/lib/sportdb/models/{stage.rb → models/stage.rb} +9 -5
  21. data/lib/sportdb/models/{stats/alltime_standing_entry.rb → models/stats/alltime_standing.rb} +9 -1
  22. data/lib/sportdb/models/{stats/event_standing_entry.rb → models/stats/event_standing.rb} +11 -1
  23. data/lib/sportdb/models/{stats/group_standing_entry.rb → models/stats/group_standing.rb} +10 -1
  24. data/lib/sportdb/models/models/team.rb +56 -0
  25. data/lib/sportdb/models/{world → models/world}/city.rb +2 -2
  26. data/lib/sportdb/models/{world → models/world}/continent.rb +0 -0
  27. data/lib/sportdb/models/{world → models/world}/country.rb +0 -0
  28. data/lib/sportdb/models/{world → models/world}/state.rb +0 -0
  29. data/lib/sportdb/{schema.rb → models/schema.rb} +165 -100
  30. data/lib/sportdb/models/stats.rb +23 -0
  31. data/lib/sportdb/models/utils.rb +24 -24
  32. data/lib/sportdb/{version.rb → models/version.rb} +12 -5
  33. data/test/helper.rb +10 -114
  34. data/test/test_changes.rb +38 -38
  35. data/test/test_cursor.rb +15 -15
  36. data/test/test_winner.rb +75 -70
  37. metadata +29 -161
  38. data/lib/sportdb/calc.rb +0 -279
  39. data/lib/sportdb/indexers/team.rb +0 -87
  40. data/lib/sportdb/mapper_teams.rb +0 -24
  41. data/lib/sportdb/matcher.rb +0 -31
  42. data/lib/sportdb/models/assoc.rb +0 -106
  43. data/lib/sportdb/models/assoc_assoc.rb +0 -15
  44. data/lib/sportdb/models/event.rb +0 -66
  45. data/lib/sportdb/models/event_ground.rb +0 -15
  46. data/lib/sportdb/models/event_team.rb +0 -16
  47. data/lib/sportdb/models/ground.rb +0 -100
  48. data/lib/sportdb/models/group_team.rb +0 -14
  49. data/lib/sportdb/models/league.rb +0 -83
  50. data/lib/sportdb/models/stage_team.rb +0 -14
  51. data/lib/sportdb/models/stats/alltime_standing.rb +0 -44
  52. data/lib/sportdb/models/stats/event_standing.rb +0 -55
  53. data/lib/sportdb/models/stats/group_standing.rb +0 -50
  54. data/lib/sportdb/models/team.rb +0 -119
  55. data/lib/sportdb/models/team_compat.rb +0 -64
  56. data/lib/sportdb/patterns.rb +0 -37
  57. data/lib/sportdb/pretty_printer.rb +0 -175
  58. data/lib/sportdb/reader.rb +0 -132
  59. data/lib/sportdb/reader_file.rb +0 -131
  60. data/lib/sportdb/reader_zip.rb +0 -172
  61. data/lib/sportdb/readers/assoc.rb +0 -54
  62. data/lib/sportdb/readers/event.rb +0 -253
  63. data/lib/sportdb/readers/event_meta.rb +0 -133
  64. data/lib/sportdb/readers/event_table.rb +0 -196
  65. data/lib/sportdb/readers/game.rb +0 -912
  66. data/lib/sportdb/readers/ground.rb +0 -53
  67. data/lib/sportdb/readers/league.rb +0 -54
  68. data/lib/sportdb/readers/season.rb +0 -83
  69. data/lib/sportdb/readers/squad_club.rb +0 -201
  70. data/lib/sportdb/readers/squad_national_team.rb +0 -173
  71. data/lib/sportdb/readers/team.rb +0 -53
  72. data/lib/sportdb/rsssf_reader.rb +0 -367
  73. data/lib/sportdb/standings.rb +0 -178
  74. data/lib/sportdb/stats.rb +0 -27
  75. data/lib/sportdb/utils.rb +0 -89
  76. data/lib/sportdb/utils_date.rb +0 -26
  77. data/lib/sportdb/utils_goals.rb +0 -20
  78. data/lib/sportdb/utils_group.rb +0 -63
  79. data/lib/sportdb/utils_map.rb +0 -44
  80. data/lib/sportdb/utils_round.rb +0 -165
  81. data/lib/sportdb/utils_scores.rb +0 -17
  82. data/lib/sportdb/utils_teams.rb +0 -48
  83. data/test/data/at-austria/2013_14/bl.txt +0 -227
  84. data/test/data/at-austria/2013_14/bl.yml +0 -30
  85. data/test/data/at-austria/2013_14/bl_ii.txt +0 -154
  86. data/test/data/at-austria/2013_14/el.txt +0 -4
  87. data/test/data/at-austria/2013_14/el.yml +0 -25
  88. data/test/data/at-austria/2013_14/squads/austria.txt +0 -40
  89. data/test/data/at-austria/2013_14/squads/salzburg.txt +0 -35
  90. data/test/data/at-austria/2014_15/1-bundesliga-ii.txt +0 -158
  91. data/test/data/at-austria/2014_15/1-bundesliga.yml +0 -18
  92. data/test/data/at-austria/2015_16/1-bundesliga-v2.conf.txt +0 -20
  93. data/test/data/at-austria/2015_16/1-bundesliga-v2.yml +0 -20
  94. data/test/data/at-austria/2015_16/1-bundesliga.conf.txt +0 -20
  95. data/test/data/at-austria/2015_16/1-bundesliga.yml +0 -24
  96. data/test/data/at-austria/2015_16/cup.yml +0 -77
  97. data/test/data/at-austria/leagues.txt +0 -11
  98. data/test/data/at-austria/teams.txt +0 -68
  99. data/test/data/at-austria/teams_2.txt +0 -21
  100. data/test/data/csv/de-2013-14--1-bundesliga.txt +0 -307
  101. data/test/data/de-deutschland/2013-14/1-bundesliga.yml +0 -26
  102. data/test/data/de-deutschland/leagues.txt +0 -4
  103. data/test/data/de-deutschland/teams.txt +0 -53
  104. data/test/data/eng-england/2015-16/1-premierleague-v2.yml +0 -2
  105. data/test/data/eng-england/2015-16/1-premierleague.yml +0 -5
  106. data/test/data/national-teams/assocs.txt +0 -231
  107. data/test/data/national-teams/europe/assocs.txt +0 -13
  108. data/test/data/national-teams/europe/teams.txt +0 -13
  109. data/test/data/national-teams/north-america/assocs.txt +0 -10
  110. data/test/data/national-teams/north-america/teams.txt +0 -7
  111. data/test/data/national-teams/teams.txt +0 -19
  112. data/test/data/players/europe/at-austria/players.txt +0 -45
  113. data/test/data/players/europe/de-deutschland/players.txt +0 -41
  114. data/test/data/players/south-america/br-brazil/players.txt +0 -51
  115. data/test/data/rsssf/at-2014-15--1-bundesliga.txt +0 -339
  116. data/test/data/rsssf/at-2015-16--1-bundesliga.txt +0 -18
  117. data/test/data/world-cup/1930/cup.txt +0 -71
  118. data/test/data/world-cup/1930/cup.yml +0 -23
  119. data/test/data/world-cup/1930/cup_goals.txt +0 -47
  120. data/test/data/world-cup/1930/cup_goals.yml +0 -23
  121. data/test/data/world-cup/1954/cup.txt +0 -90
  122. data/test/data/world-cup/1954/cup.yml +0 -30
  123. data/test/data/world-cup/1962/cup.txt +0 -86
  124. data/test/data/world-cup/1962/cup.yml +0 -32
  125. data/test/data/world-cup/1974/cup.yml +0 -35
  126. data/test/data/world-cup/1974/cup_finals.txt +0 -14
  127. data/test/data/world-cup/1974/cup_i.txt +0 -55
  128. data/test/data/world-cup/1974/cup_ii.txt +0 -34
  129. data/test/data/world-cup/2014/cup.txt +0 -5
  130. data/test/data/world-cup/2014/cup.yml +0 -54
  131. data/test/data/world-cup/2014/squads/br-brazil.txt +0 -46
  132. data/test/data/world-cup/2014/squads/de-deutschland.txt +0 -8
  133. data/test/data/world-cup/2014/squads/jp-japan.txt +0 -30
  134. data/test/data/world-cup/2014/squads/uy-uruguay.txt +0 -32
  135. data/test/data/world-cup/leagues.txt +0 -5
  136. data/test/data/world-cup/seasons_1930.txt +0 -4
  137. data/test/data/world-cup/seasons_1954.txt +0 -4
  138. data/test/data/world-cup/seasons_1962.txt +0 -4
  139. data/test/data/world-cup/seasons_1974.txt +0 -5
  140. data/test/data/world-cup/teams_1930.txt +0 -26
  141. data/test/data/world-cup/teams_1954.txt +0 -30
  142. data/test/data/world-cup/teams_1962.txt +0 -29
  143. data/test/data/world-cup/teams_1974.txt +0 -29
  144. data/test/test_assoc_reader.rb +0 -199
  145. data/test/test_event_meta_reader.rb +0 -47
  146. data/test/test_event_reader.rb +0 -64
  147. data/test/test_event_table_reader.rb +0 -57
  148. data/test/test_goals.rb +0 -107
  149. data/test/test_indexer_team.rb +0 -34
  150. data/test/test_load.rb +0 -61
  151. data/test/test_pp.rb +0 -35
  152. data/test/test_reader.rb +0 -88
  153. data/test/test_reader_from_string.rb +0 -63
  154. data/test/test_round_auto.rb +0 -370
  155. data/test/test_round_def.rb +0 -109
  156. data/test/test_round_header.rb +0 -183
  157. data/test/test_rsssf_reader.rb +0 -76
  158. data/test/test_squad_club_reader.rb +0 -76
  159. data/test/test_squad_national_team_reader.rb +0 -116
  160. data/test/test_standings.rb +0 -279
  161. data/test/test_standings_ii.rb +0 -46
  162. data/test/test_utils.rb +0 -124
@@ -1,172 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module SportDb
4
-
5
-
6
- class ZipReader < ReaderBase
7
-
8
- def initialize( name, include_path, opts = {} )
9
-
10
- ## todo/fix: make include_path an opts (included in opts?) - why? why not??
11
-
12
- path = "#{include_path}/#{name}.zip"
13
- ## todo: check if zip exists
14
-
15
- @zip_file = Zip::File.open( path ) ## NOTE: do NOT create if file is missing; let it crash
16
-
17
- ### allow prefix (path) in name
18
- ### e.g. assume all files relative to setup manifest
19
- ## e.g. at-austria-master/setups/all.txt or
20
- ## be-belgium-master/setups/all.txt
21
- ## for
22
- ## setups/all.txt
23
- ###
24
- ## will get (re)set w/ fixture/setup reader
25
- ##
26
- ## todo/fix: change/rename to @relative_path ?? - why? why not?
27
- @zip_prefix = ''
28
- end
29
-
30
- def close
31
- ## todo/check: add a close method - why? why not ???
32
- @zip_file.close
33
- end
34
-
35
-
36
- def create_fixture_reader( name )
37
- ## e.g. pass in => setups/all or setups/test etc. e.g. w/o .txt extension
38
- query = "**/#{name}.txt"
39
-
40
- ## note: returns an array of Zip::Entry
41
- candidates = @zip_file.glob( query )
42
- pp candidates
43
-
44
- ## use first candidates entry as match
45
- ## todo/fix: issue warning if more than one entries/matches!!
46
-
47
- ## get fullpath e.g. at-austria-master/setups/all.txt
48
- path = candidates[0].name
49
- logger.debug " zip entry path >>#{path}<<"
50
-
51
- ## cut-off at-austria-master/ NOTE: includes trailing slash (if present)
52
- ## logger.debug " path.size #{path.size} >>#{path}<<"
53
- ## logger.debug " name.size #{name.size+4} >>#{name}<<"
54
-
55
- ## note: add +4 for extension (.txt)
56
- @zip_prefix = path[ 0...(path.size-(name.size+4)) ]
57
- logger.debug " zip entry prefix >>#{@zip_prefix}<<"
58
-
59
- logger.info "parsing data in zip '#{name}' (#{path})..."
60
-
61
- FixtureReader.from_zip( @zip_file, path )
62
- end
63
-
64
- def create_club_squad_reader( name, more_attribs={} )
65
- path = name_to_zip_entry_path( name )
66
-
67
- logger.info "parsing data in zip (club squad) '#{name}' (#{path})..."
68
- ClubSquadReader.from_zip( @zip_file, path, more_attribs )
69
- end
70
-
71
- def create_national_team_squad_reader( name, more_attribs={} )
72
- path = name_to_zip_entry_path( name )
73
-
74
- logger.info "parsing data in zip (national team squad) '#{name}' (#{path})..."
75
- NationalTeamSquadReader.from_zip( @zip_file, path, more_attribs )
76
- end
77
-
78
- def create_season_reader( name )
79
- path = name_to_zip_entry_path( name )
80
-
81
- logger.info "parsing data in zip (season) '#{name}' (#{path})..."
82
- SeasonReader.from_zip( @zip_file, path )
83
- end
84
-
85
- def create_assoc_reader( name )
86
- path = name_to_zip_entry_path( name )
87
-
88
- logger.info "parsing data in zip (assoc) '#{name}' (#{path})..."
89
- AssocReader.from_zip( @zip_file, path )
90
- end
91
-
92
- def create_ground_reader( name, more_attribs={} )
93
- path = name_to_zip_entry_path( name )
94
-
95
- logger.info "parsing data in zip (ground) '#{name}' (#{path})..."
96
- GroundReader.from_zip( @zip_file, path, more_attribs )
97
- end
98
-
99
- def create_league_reader( name, more_attribs={} )
100
- path = name_to_zip_entry_path( name )
101
-
102
- logger.info "parsing data in zip (league) '#{name}' (#{path})..."
103
- LeagueReader.from_zip( @zip_file, path, more_attribs )
104
- end
105
-
106
- def create_team_reader( name, more_attribs={} )
107
- path = name_to_zip_entry_path( name )
108
-
109
- logger.info "parsing data in zip (team) '#{name}' (#{path})..."
110
- TeamReader.from_zip( @zip_file, path, more_attribs )
111
- end
112
-
113
- def create_event_reader( name, more_attribs={} )
114
- path = name_to_zip_entry_path( name, '.yml' ) ## NOTE: use .yml extension
115
-
116
- logger.info "parsing data in zip (event) '#{name}' (#{path})..."
117
- EventReader.from_zip( @zip_file, path, more_attribs )
118
- end
119
-
120
- def create_event_table_reader( name, more_attribs={} )
121
- path = name_to_zip_entry_path( name, '.txt' ) ## NOTE: use .txt extension
122
-
123
- logger.info "parsing data in zip (event table) '#{name}' (#{path})..."
124
- EventTableReader.from_zip( @zip_file, path, more_attribs )
125
- end
126
-
127
- def create_game_reader( name, more_attribs={} )
128
- ## NOTE: pass in .yml as path (that is, event config!!!!)
129
- path = name_to_zip_entry_path( name, '.yml' ) ## NOTE: use .yml extension
130
-
131
- logger.info "parsing data in zip (fixture) '#{name}' (#{path})..."
132
- GameReader.from_zip( @zip_file, path, more_attribs )
133
- end
134
-
135
-
136
- def create_person_reader( name, more_attribs={} )
137
- ## fix-fix-fix: change to new format e.g. from_file, from_zip etc!!!
138
- ## reader = PersonDb::PersonReader.new( include_path )
139
- # reader.read( name, country_id: country.id )
140
- end
141
-
142
- private
143
-
144
- def path_to_real_path( path )
145
- # map name to name_real_path
146
- # name might include !/ for virtual path (gets cut off)
147
- # e.g. at-austria!/w-wien/beers becomse w-wien/beers
148
- pos = path.index( '!/')
149
- if pos.nil?
150
- path # not found; real path is the same as name
151
- else
152
- # cut off everything until !/ e.g.
153
- # at-austria!/w-wien/beers becomes
154
- # w-wien/beers
155
- path[ (pos+2)..-1 ]
156
- end
157
- end
158
-
159
- def name_to_zip_entry_path( name, extension='.txt' )
160
- path = "#{name}#{extension}"
161
-
162
- real_path = path_to_real_path( path )
163
-
164
- # NOTE: add possible zip entry prefix path
165
- # (if present includes trailing slash e.g. /)
166
- entry_path = "#{@zip_prefix}#{real_path}"
167
- entry_path
168
- end
169
-
170
- end # class ZipReader
171
-
172
- end # module SportDb
@@ -1,54 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- module SportDb
4
-
5
-
6
- class AssocReader
7
-
8
- include LogUtils::Logging
9
-
10
- ## make models available by default with namespace
11
- # e.g. lets you use Usage instead of Model::Usage
12
- include Models
13
-
14
-
15
- def self.from_zip( zip_file, entry_path, more_attribs={} )
16
- ## get text content from zip
17
- entry = zip_file.find_entry( entry_path )
18
-
19
- text = entry.get_input_stream().read()
20
- text = text.force_encoding( Encoding::UTF_8 )
21
-
22
- self.from_string( text, more_attribs )
23
- end
24
-
25
- def self.from_file( path, more_attribs={} )
26
- ## note: assume/enfore utf-8 encoding (with or without BOM - byte order mark)
27
- ## - see textutils/utils.rb
28
- text = File.read_utf8( path )
29
- self.from_string( text, more_attribs )
30
- end
31
-
32
- def self.from_string( text, more_attribs={} )
33
- AssocReader.new( text, more_attribs )
34
- end
35
-
36
-
37
- def initialize( text, more_attribs={} )
38
- ## todo/fix: how to add opts={} ???
39
- @text = text
40
- @more_attribs = more_attribs
41
- end
42
-
43
-
44
- def read
45
- reader = ValuesReader.from_string( @text, @more_attribs )
46
-
47
- reader.each_line do |new_attributes, values|
48
- Assoc.create_or_update_from_values( new_attributes, values )
49
- end # each lines
50
- end
51
-
52
-
53
- end # class AssocReader
54
- end # module SportDb
@@ -1,253 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- module SportDb
4
-
5
-
6
- class EventReader
7
-
8
- include LogUtils::Logging
9
-
10
- ## make models available by default with namespace
11
- # e.g. lets you use Usage instead of Model::Usage
12
- include Models
13
-
14
- attr_reader :event # returns event record; call read first
15
- attr_reader :fixtures # fixtures/sources entry from event config
16
-
17
- def self.from_zip( zip_file, entry_path, more_attribs={} )
18
- ## get text content from zip
19
- entry = zip_file.find_entry( entry_path )
20
-
21
- text = entry.get_input_stream().read()
22
- text = text.force_encoding( Encoding::UTF_8 )
23
-
24
- # basename of .yml file
25
- config = File.basename( entry_path )
26
-
27
- self.from_string( text, config, more_attribs )
28
- end
29
-
30
- def self.from_file( path, more_attribs={} )
31
- ## note: assume/enfore utf-8 encoding (with or without BOM - byte order mark)
32
- ## - see textutils/utils.rb
33
- text = File.read_utf8( path )
34
-
35
- # basename of .yml file
36
- config = File.basename( path )
37
-
38
- self.from_string( text, config, more_attribs )
39
- end
40
-
41
- def self.from_string( text, config, more_attribs={} )
42
- self.new( text, config, more_attribs )
43
- end
44
-
45
- def initialize( text, config, more_attribs={} )
46
- ## todo/fix: how to add opts={} ???
47
- @text = text
48
- @more_attribs = more_attribs ## todo/check: not used for now? (remove - why, why not??)
49
-
50
- @config = config # name of event configuration (relative basename w/o path or string)
51
-
52
- @event = nil
53
- @fixtures = []
54
- end
55
-
56
-
57
- def read
58
- @event = nil # reset cached event rec
59
- @fixtures = [] # reset cached fixtures
60
-
61
- ####
62
- ## fix!!!!!
63
- ## use Event.create_or_update_from_hash or similar
64
- ## use Event.create_or_update_from_hash_reader?? or similar
65
- # move parsing code to model
66
-
67
- reader = HashReader.from_string( @text )
68
-
69
- event_attribs = {}
70
-
71
- ## set default sources to basename by convention
72
- # e.g 2013_14/bl => bl
73
- # etc.
74
- # use fixtures/sources: to override default
75
-
76
- event_attribs[ 'config' ] = @config # name of .yml file
77
-
78
- reader.each_typed do |key, value|
79
-
80
- ## puts "processing event attrib >>#{key}<< >>#{value}<<..."
81
-
82
- if key.downcase == 'league' ## note: allow league, League, etc.
83
- league_key = value.to_s.strip
84
- ## check if league_key includes uppercase letters (e.g. Deutsche Bundesliga and NOT de etc.)
85
- if league_key =~ /[A-Z]/
86
- ## assume league name (NOT league key); try to lookup leauge key in database
87
- league = League.find_by( title: league_key )
88
- ## todo: add synonyms/alt names - why? why not??
89
- else
90
- ## assume "verbatim/literal" team_key (use as is 1:1)
91
- league = League.find_by( key: league_key )
92
- end
93
-
94
- ## check if it exists
95
- if league.present? ## todo: just use if league (no present?) ???
96
- event_attribs['league_id'] = league.id
97
- else
98
- logger.error "league with key >>#{league_key}<< missing"
99
- exit 1
100
- end
101
-
102
- elsif key.downcase == 'season' ## note: allow season, Season, etc.
103
- season_key = value.to_s.strip
104
- season = Season.find_by( key: season_key )
105
-
106
- ## check if it exists
107
- if season.present?
108
- event_attribs['season_id'] = season.id
109
- else
110
- logger.error "season with key >>#{season_key}<< missing"
111
- exit 1
112
- end
113
-
114
- elsif key.downcase == 'start_at' || key.downcase == 'begin_at' || key.downcase == 'start date'
115
-
116
- if value.is_a?(DateTime) || value.is_a?(Date)
117
- start_at = value
118
- else # assume it's a string
119
- start_at = DateTime.strptime( value.to_s.strip, '%Y-%m-%d' )
120
- end
121
-
122
- event_attribs['start_at'] = start_at
123
-
124
- elsif key.downcase == 'end_at' || key.downcase == 'stop_at'
125
-
126
- if value.is_a?(DateTime) || value.is_a?(Date)
127
- end_at = value
128
- else # assume it's a string
129
- end_at = DateTime.strptime( value.to_s.strip, '%Y-%m-%d' )
130
- end
131
-
132
- event_attribs['end_at'] = end_at
133
-
134
- elsif key.downcase == 'grounds' || key.downcase == 'stadiums' || key.downcase == 'venues'
135
- ## assume grounds value is an array
136
-
137
- ##
138
- ## note: for now we allow invalid ground keys
139
- ## will skip keys not found
140
-
141
- ground_ids = []
142
- value.each do |item|
143
- ground_key = item.to_s.strip
144
- ground = Ground.find_by( key: ground_key )
145
- if ground.nil?
146
- puts "[warn] ground/stadium w/ key >#{ground_key}< not found; skipping ground"
147
- else
148
- ground_ids << ground.id
149
- end
150
- end
151
-
152
- event_attribs['ground_ids'] = ground_ids
153
-
154
- elsif key.downcase == 'team3' ## note: check before teams (to avoid future gotchas)
155
- ## for now always assume false # todo: fix - use value and convert to boolean if not boolean
156
- event_attribs['team3'] = false
157
-
158
- elsif key.downcase =~ /teams/ ## note: allow teams, Teams, 18 teams, 18 Teams etc.
159
- ## assume teams value is an array
160
-
161
- ### check if key includes number of teams; if yes - use for checksum/assert
162
- if key =~ /(\d+)/
163
- if value.size != $1.to_i
164
- puts "[fatal] event reader - team key - expecting #{$1.to_i} teams; got #{value.size}"
165
- exit 1
166
- end
167
- end
168
-
169
- team_ids = []
170
- value.each do |item|
171
- team_key = item.to_s.strip
172
-
173
- ## check if team_key includes uppercase letters
174
- if team_key =~ /[A-Z]/
175
- ## assume team name (NOT team key); try to lookup team key in database
176
- ## todo/fix:
177
- ## remove subtitle from title e.g. everything in ()
178
- ## SV Oberwart (RL Ost) => SV Oberwart
179
- team = Team.find_by( title: team_key )
180
- if team.nil?
181
- ## next try synonyms
182
- team = Team.where( "synonyms LIKE ?", "%#{team_key}%" ).first
183
- end
184
- else
185
- ## assume "verbatim/literal" team_key (use as is 1:1)
186
- team = Team.find_by( key: team_key )
187
- end
188
-
189
- if team.nil?
190
- ### print better error message than just
191
- ## *** error: Couldn't find SportDb::Model::Team
192
- puts "[fatal] event reader - team keys: #{value.inspect}"
193
- puts "[fatal] event reader - record for team key >#{team_key}< not found"
194
- exit 1
195
- ### fix/todo: throw exception/error
196
- end
197
-
198
- team_ids << team.id
199
- end
200
-
201
- event_attribs['team_ids'] = team_ids
202
-
203
- elsif key.downcase == 'fixtures' || key.downcase == 'sources'
204
- ### todo: check for mulitiple fixtures/sources ?? allow disallow?? why? why not?
205
- if value.kind_of?(Array)
206
- event_attribs['sources'] = value.join(',')
207
- @fixtures += value
208
- else # assume plain (single fixture) string
209
- event_attribs['sources'] = value.to_s
210
- @fixtures << value.to_s
211
- end
212
- else
213
- ## todo: add a source location struct to_s or similar (file, line, col)
214
- logger.error "unknown event attrib #{key}; skipping attrib"
215
- end
216
-
217
- end # each key,value
218
-
219
- if @fixtures.empty?
220
- ## use basename of config file as default (without extension)
221
- sources_default = File.basename( @config, File.extname( @config ) )
222
- @fixtures << sources_default
223
- event_attribs[ 'sources' ] = sources_default
224
- end
225
-
226
-
227
- league_id = event_attribs['league_id']
228
- season_id = event_attribs['season_id']
229
-
230
- logger.debug "find event - league_id: #{league_id}, season_id: #{season_id}"
231
-
232
- event = Event.find_by( league_id: league_id,
233
- season_id: season_id )
234
-
235
- ## check if it exists
236
- if event.present?
237
- logger.debug "*** update event #{event.id}-#{event.key}:"
238
- else
239
- logger.debug "*** create event:"
240
- event = Event.new
241
- end
242
-
243
- logger.debug event_attribs.to_json
244
-
245
- event.update_attributes!( event_attribs )
246
-
247
- # keep a cached reference for later use
248
- @event = event
249
- end # method read
250
-
251
-
252
- end # class EventReader
253
- end # module SportDb