Class: SportDb::MapperV2

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/sportdb/formats/match/mapper.rb

Overview

todo/check: rename to NameMapper ? why? why not??

Defined Under Namespace

Classes: MappingStruct, Record

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records_or_mapping, tag) ⇒ MapperV2

Returns a new instance of MapperV2.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sportdb/formats/match/mapper.rb', line 58

def initialize( records_or_mapping, tag )
  ## for convenience allow easy (auto-)convert text (lines) to records
  ##  as 1) text block/string  or
  ##     2) array of lines/strings
  records_or_mapping = build_records( records_or_mapping )   if records_or_mapping.is_a?( String ) ||
                                                                (records_or_mapping.is_a?( Array ) && records_or_mapping[0].is_a?( String ))

  ## build mapping lookup table
  @known_names =  if records_or_mapping.is_a?( Hash )  ## assume "custom" mapping hash table (name=>record)
                      build_name_table_for_mapping( records_or_mapping )
                   else  ## assume array of records
                      build_name_table_for_records( records_or_mapping )
                   end

  ## build lookup hash by record (e.g. team/club/etc.) key
  records = if records_or_mapping.is_a?( Array )
                records_or_mapping
            else   ## assume hash (uses values assuming to be all records - note might include duplicates)
                records_or_mapping.values
            end

  @records = records.reduce({}) { |h,rec| h[rec.key]=rec; h }


  ## todo: rename tag to attrib or attrib_name - why ?? why not ???
  @tag = tag   # e.g. tag name use for @@brewery@@ @@team@@ etc.
end

Instance Attribute Details

#known_namesObject (readonly)

rename to mapping or mappings or just names - why? why not?



14
15
16
# File 'lib/sportdb/formats/match/mapper.rb', line 14

def known_names
  @known_names
end

Instance Method Details

#build_records(txt_or_lines) ⇒ Object



25
26
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
# File 'lib/sportdb/formats/match/mapper.rb', line 25

def build_records( txt_or_lines )
  recs = []

  if txt_or_lines.is_a?( String )
      ## todo/fix: use ParserHelper read_lines !!! ????
      txt = txt_or_lines
      lines = []

      txt.each_line do |line|
        line = line.strip

        next if line.empty? || line.start_with?( '#' )  ## note: skip empty and comment lines
        lines << line
      end
  else
      lines = txt_or_lines
  end

  lines.each do |line|
    values = line.split( '|' )
    values = values.map { |value| value.strip }

    name      = values[0]
    ## note: quick hack - auto-generate key, that is, remove all non-ascii chars and downcase
    key       = name.downcase.gsub( /[^a-z]/, '' )
    alt_names = values.size > 1 ? values[1..-1].join( '|' ) : nil

    recs << Record.new( key, name, alt_names )
  end
  recs
end

#find_rec!(line) ⇒ Object



94
95
96
# File 'lib/sportdb/formats/match/mapper.rb', line 94

def find_rec!( line )
  find_rec_for!( @tag, line, @records )
end

#find_recs!(line) ⇒ Object

note: keys (plural!) - will return array



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sportdb/formats/match/mapper.rb', line 98

def find_recs!( line )  # note: keys (plural!) - will return array
  counter = 1
  recs = []

  rec = find_rec_for!( "#{@tag}#{counter}", line, @records )
  while rec
    recs << rec
    counter += 1
    rec = find_rec_for!( "#{@tag}#{counter}", line, @records )
  end
  recs
end

#map_names!(line) ⇒ Object

rename to just map! - why?? why not???



88
89
90
91
92
# File 'lib/sportdb/formats/match/mapper.rb', line 88

def map_names!( line )   ## rename to just map! - why?? why not???
  begin
    found = map_name_for!( @tag, line, @known_names )
  end while found
end