Class: SportDb::Import::ClubPropsReader

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/formats/team/club_reader_props.rb

Constant Summary collapse

NA_VARIANTS =
['-', '--', '---',
'?', '??', '???',
'_', '__', '___',
'na', 'n/a',
'nil', 'null']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt) ⇒ ClubPropsReader

Returns a new instance of ClubPropsReader.



23
24
25
# File 'lib/sportdb/formats/team/club_reader_props.rb', line 23

def initialize( txt )
  @txt = txt
end

Class Method Details

.parse(txt) ⇒ Object



18
19
20
# File 'lib/sportdb/formats/team/club_reader_props.rb', line 18

def self.parse( txt )
  new( txt ).parse
end

.read(path) ⇒ Object

use - rename to read_file or from_file etc. - why? why not?



13
14
15
16
# 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

Instance Method Details

#catalogObject



10
# File 'lib/sportdb/formats/team/club_reader_props.rb', line 10

def catalog() Import.catalog; end

#is_na?(col) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# 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) ⇒ Boolean

allow various values for nil or n/a (not available/applicable) for now

add more or less - why? why not?

Returns:

  • (Boolean)


74
# File 'lib/sportdb/formats/team/club_reader_props.rb', line 74

def is_not_na?( col ) !is_na?( col); end

#parseObject



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
# 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