Class: Temporal::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/temporal/parser.rb,
lib/temporal/parser/english.rb,
lib/temporal/parser/afrikaans.rb

Constant Summary collapse

@@literals =
{}

Class Method Summary collapse

Class Method Details

.add_literal(literal, &block) ⇒ Object



5
6
7
# File 'lib/temporal/parser.rb', line 5

def self.add_literal literal, &block
  @@literals[ literal ] = block
end

.match?(string) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
# File 'lib/temporal/parser.rb', line 9

def self.match? string
  to_match = string.downcase.strip
  @@literals.keys.each do |key|
    return true if to_match =~ key
  end
  return false
end

.parse(string) ⇒ Object



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

def self.parse string
  @@literals.keys.each do |key|
    match = string.match(key)
    next unless match
    result = []
    result << parse(match.pre_match) if match.pre_match.strip.size > 0
    result << @@literals[key].call
    result << parse(match.post_match) if match.post_match.strip.size > 0
    result.flatten!
    if result.empty?
      result = nil
    else
      result = result.pop if result.size == 1
    end
    return result
  end
  string
end