Class: ScoreFormats::ScoreParser

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/score-formats/parser.rb

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(lang:) ⇒ ScoreParser

Returns a new instance of ScoreParser.



8
9
10
11
12
13
14
# File 'lib/score-formats/parser.rb', line 8

def initialize( lang: )
  @lang    = lang.to_sym   ## note: make sure lang is always a symbol for now (NOT a string)

  ## fallback to english if lang not available
  ##  todo/fix: add/issue warning - why? why not?
  @formats = FORMATS[ @lang ] || FORMATS[ :en ]
end

Instance Method Details

#find!(line) ⇒ Object



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
70
71
# File 'lib/score-formats/parser.rb', line 40

def find!( line )
  ### fix: add and match all-in-one literal first, followed by

  # note: always call after find_dates !!!
  #  scores match date-like patterns!!  e.g. 10-11  or 10:00 etc.
  #   -- note: score might have two digits too

  ### fix: depending on language allow 1:1 or 1-1
  ##   do NOT allow mix and match
  ##  e.g. default to en is  1-1
  ##    de is 1:1 etc.


  # extract score from line
  # and return it
  # note: side effect - removes date from line string

  score = nil
  @formats.each do |format|
    re  = format[0]
    tag = format[1]
    m = re.match( line )
    if m
      score = parse_matchdata( m )
      line.sub!( m[0], tag )
      break
    end
    # no match; continue; try next regex pattern
  end

  score  # note: nil if no match found
end

#parse(line) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/score-formats/parser.rb', line 17

def parse( line )

   ##########
   ## todo/fix/check: add unicode to regular dash conversion - why? why not?
   ##  e.g. – becomes -  (yes, the letters a different!!!)
   #############

  score = nil
  @formats.each do |format|
    re = format[0]
    m = re.match( line )
    if m
      score = parse_matchdata( m )
      break
    end
    # no match; continue; try next regex pattern
  end

  ## todo/fix - raise ArgumentError - invalid score; no format match found
  score  # note: nil if no match found
end