lib/sportdb/readers/event.rb in sportdb-models-1.15.2 vs lib/sportdb/readers/event.rb in sportdb-models-1.16.0
- old
+ new
@@ -19,45 +19,46 @@
entry = zip_file.find_entry( entry_path )
text = entry.get_input_stream().read()
text = text.force_encoding( Encoding::UTF_8 )
- config = File.basename( entry_path ) # name a of .yml file
+ # basename of .yml file
+ config = File.basename( entry_path )
self.from_string( text, config, more_attribs )
end
def self.from_file( path, more_attribs={} )
## note: assume/enfore utf-8 encoding (with or without BOM - byte order mark)
## - see textutils/utils.rb
text = File.read_utf8( path )
- config = File.basename( name ) # name a of .yml file
+ # basename of .yml file
+ config = File.basename( path )
self.from_string( text, config, more_attribs )
end
def self.from_string( text, config, more_attribs={} )
- EventReader.new( text, config, more_attribs )
+ self.new( text, config, more_attribs )
end
def initialize( text, config, more_attribs={} )
## todo/fix: how to add opts={} ???
@text = text
- @more_attribs = more_attribs
+ @more_attribs = more_attribs ## todo/check: not used for now? (remove - why, why not??)
- @config = config # name of event configuration (relative basename w/o path or string)
- @sources_default = config # note: use same a config for now
+ @config = config # name of event configuration (relative basename w/o path or string)
@event = nil
@fixtures = []
end
def read
- @fixtures = [] # reset cached fixtures
@event = nil # reset cached event rec
+ @fixtures = [] # reset cached fixtures
####
## fix!!!!!
## use Event.create_or_update_from_hash or similar
## use Event.create_or_update_from_hash_reader?? or similar
@@ -70,12 +71,11 @@
## set default sources to basename by convention
# e.g 2013_14/bl => bl
# etc.
# use fixtures/sources: to override default
- event_attribs[ 'sources' ] = @sources_default
- event_attribs[ 'config' ] = @config # name a of .yml file
+ event_attribs[ 'config' ] = @config # name of .yml file
reader.each_typed do |key, value|
## puts "processing event attrib >>#{key}<< >>#{value}<<..."
@@ -109,31 +109,31 @@
else
logger.error "season with key >>#{season_key}<< missing"
exit 1
end
- elsif key == 'start_at' || key == 'begin_at' || key.downcase == 'start date'
+ elsif key.downcase == 'start_at' || key.downcase == 'begin_at' || key.downcase == 'start date'
if value.is_a?(DateTime) || value.is_a?(Date)
start_at = value
else # assume it's a string
start_at = DateTime.strptime( value.to_s.strip, '%Y-%m-%d' )
end
event_attribs['start_at'] = start_at
- elsif key == 'end_at' || key == 'stop_at'
+ elsif key.downcase == 'end_at' || key.downcase == 'stop_at'
if value.is_a?(DateTime) || value.is_a?(Date)
end_at = value
else # assume it's a string
end_at = DateTime.strptime( value.to_s.strip, '%Y-%m-%d' )
end
event_attribs['end_at'] = end_at
- elsif key == 'grounds' || key == 'stadiums' || key == 'venues'
+ elsif key.downcase == 'grounds' || key.downcase == 'stadiums' || key.downcase == 'venues'
## assume grounds value is an array
##
## note: for now we allow invalid ground keys
## will skip keys not found
@@ -149,11 +149,11 @@
end
end
event_attribs['ground_ids'] = ground_ids
- elsif key == 'team3' ## note: check before teams (to avoid future gotchas)
+ elsif key.downcase == 'team3' ## note: check before teams (to avoid future gotchas)
## for now always assume false # todo: fix - use value and convert to boolean if not boolean
event_attribs['team3'] = false
elsif key.downcase =~ /teams/ ## note: allow teams, Teams, 18 teams, 18 Teams etc.
## assume teams value is an array
@@ -198,11 +198,11 @@
team_ids << team.id
end
event_attribs['team_ids'] = team_ids
- elsif key == 'fixtures' || key == 'sources'
+ elsif key.downcase == 'fixtures' || key.downcase == 'sources'
### todo: check for mulitiple fixtures/sources ?? allow disallow?? why? why not?
if value.kind_of?(Array)
event_attribs['sources'] = value.join(',')
@fixtures += value
else # assume plain (single fixture) string
@@ -213,9 +213,17 @@
## todo: add a source location struct to_s or similar (file, line, col)
logger.error "unknown event attrib #{key}; skipping attrib"
end
end # each key,value
+
+ if @fixtures.empty?
+ ## use basename of config file as default (without extension)
+ sources_default = File.basename( @config, File.extname( @config ) )
+ @fixtures << sources_default
+ event_attribs[ 'sources' ] = sources_default
+ end
+
league_id = event_attribs['league_id']
season_id = event_attribs['season_id']
logger.debug "find event - league_id: #{league_id}, season_id: #{season_id}"