Class: DateFormats::DateParser

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

Direct Known Subclasses

RsssfDateParser

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(lang:, formats: nil, month_names: nil, day_names: nil) ⇒ DateParser

Returns a new instance of DateParser.



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
# File 'lib/date-formats/parser.rb', line 46

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

  if formats
    @formats = formats
  else
    @formats = FORMATS[ @lang ]
    if @formats
      month_names = MONTH_NAMES[ @lang ]
      day_names   = DAY_NAMES[ @lang ]
    else
      ## fallback to english if lang not available
      ##  todo/fix: add/issue warning!!!!!
      @formats    = FORMATS[ :en ]
      month_names = MONTH_NAMES[ :en ]
      day_names   = DAY_NAMES[ :en ]
    end
  end

  ## convert month_names and day_names to map if present
  @month_names = month_names ? build_map( month_names ) : nil
  @day_names   = day_names   ? build_map( day_names )   : nil
end

Instance Method Details

#find!(line, start:) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/date-formats/parser.rb', line 90

def find!( line, start: )
  # fix: use more lookahead for all required trailing spaces!!!!!
  # fix: use <name capturing group> for month,day,year etc.!!!

  #
  # fix: !!!!
  #   date in [] will become [[DATE.DE4]] - when getting removed will keep ]!!!!
  #   fix: change regex to \[[A-Z0-9.]\]  !!!!!!  plus add unit test too!!!
  #

  date = nil
  @formats.each do |format|
    re  = format[0]
    tag = format[1]
    m = re.match( line )
    if m
      date = parse_matchdata( m, start: start )
      ## fix: use md[0] e.g. match for sub! instead of using regex again - why? why not???
      ## fix: use md.begin(0), md.end(0)
      line.sub!( m[0], tag )
      ## todo/fix: make sure match data will not get changed (e.g. using sub! before parse_date_time)
      break
    end
    # no match; continue; try next regex pattern
  end
  date  # note: nil if no match found
end

#parse(line, start:) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/date-formats/parser.rb', line 73

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

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