class SportDb::Import::ClubPropsReader
Constants
- NA_VARIANTS
Public Class Methods
new( txt )
click to toggle source
# File lib/sportdb/formats/team/club_reader_props.rb, line 23 def initialize( txt ) @txt = txt end
parse( txt )
click to toggle source
# File lib/sportdb/formats/team/club_reader_props.rb, line 18 def self.parse( txt ) new( txt ).parse end
read( path )
click to toggle source
# File lib/sportdb/formats/team/club_reader_props.rb, line 13 def self.read( path ) ## use - rename to read_file or from_file etc. - why? why not? txt = File.open( path, 'r:utf-8' ) {|f| f.read } parse( txt ) end
Public Instance Methods
catalog()
click to toggle source
# File lib/sportdb/formats/team/club_reader_props.rb, line 10 def catalog() Import.catalog; end
is_na?( col )
click to toggle source
# File lib/sportdb/formats/team/club_reader_props.rb, line 82 def is_na?( col ) col.nil? || col.empty? || NA_VARIANTS.include?( col.downcase ) end
is_not_na?( col )
click to toggle source
allow various values for nil or n/a (not available/applicable) for now
add more or less - why? why not?
# File lib/sportdb/formats/team/club_reader_props.rb, line 74 def is_not_na?( col ) !is_na?( col); end
parse()
click to toggle source
# File lib/sportdb/formats/team/club_reader_props.rb, line 27 def parse recs = parse_csv( @txt ) recs.each do |rec| name = rec['Name'] if name.nil? puts "** !!! ERROR !!! Name column required / missing / NOT found in row:" pp rec exit 1 end ## find / match club by (canocial) name m = catalog.clubs.match( name ) if m.size > 1 puts "** !!! WARN !!! ambigious (multiple) club matches (#{m.size}) for name >#{name}< in props row:" pp rec pp m ## todo/fix: try filter by canonical name if more than one match m = m.select { |club| club.name == name } end if m.empty? puts "** !!! ERROR !!! no club match for (canonical) name >#{name}< in props row:" pp rec exit 1 elsif m.size > 1 puts "** !!! ERROR !!! ambigious (multiple) club matches (#{m.size}) for (canonical) name >#{name}< in props row:" pp rec pp m exit 1 else ## assume size == 1, bingo!!! club_rec = m[0] ## todo/fix: warn if name differes from (canonical) name ## todo/fix: also add props to in-memory structs/records!!! ## todo/fix: only updated "on-demand" from in-memory struct/records!!!! ## update attributes club_rec.key = rec['Key'] if is_not_na?( rec['Key'] ) club_rec.code = rec['Code'] if is_not_na?( rec['Code'] ) ## todo/fix: add (some) more props e.g. address, web, etc. end end end