Class: Chronic::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/chronic/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, handler_method) ⇒ Handler

Returns a new instance of Handler.

Parameters:

  • pattern (Array)

    A list of patterns to match tokens against

  • handler_method (Symbol)

    The method to be invoked when patterns are matched. This method should exist inside the Chronic::Handlers module



14
15
16
17
# File 'lib/chronic/handler.rb', line 14

def initialize(pattern, handler_method)
  @pattern = pattern
  @handler_method = handler_method
end

Instance Attribute Details

#handler_methodSymbol (readonly)

Returns The method which handles this list of patterns. This method should exist inside the Chronic::Handlers module.

Returns:

  • (Symbol)

    The method which handles this list of patterns. This method should exist inside the Chronic::Handlers module



9
10
11
# File 'lib/chronic/handler.rb', line 9

def handler_method
  @handler_method
end

#patternArray (readonly)

Returns A list of patterns.

Returns:

  • (Array)

    A list of patterns



5
6
7
# File 'lib/chronic/handler.rb', line 5

def pattern
  @pattern
end

Instance Method Details

#==(other) ⇒ Boolean

Returns True if these handlers match.

Parameters:

  • The (Handler)

    handler to compare

Returns:

  • (Boolean)

    True if these handlers match



75
76
77
# File 'lib/chronic/handler.rb', line 75

def ==(other)
  @pattern == other.pattern
end

#invoke(type, tokens, options) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/chronic/handler.rb', line 64

def invoke(type, tokens, options)
  if Chronic.debug
    puts "-#{type}"
    puts "Handler: #{@handler_method}"
  end

  Handlers.send(@handler_method, tokens, options)
end

#match(tokens, definitions) ⇒ Boolean

Parameters:

  • tokens (Array)
  • definitions (Hash)

Returns:

  • (Boolean)

See Also:

  • Chronic.tokens_to_span


23
24
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
56
57
58
59
60
61
62
# File 'lib/chronic/handler.rb', line 23

def match(tokens, definitions)
  token_index = 0

  @pattern.each do |element|
    name = element.to_s
    optional = name[-1, 1] == '?'
    name = name.chop if optional

    case element
    when Symbol
      if tags_match?(name, tokens, token_index)
        token_index += 1
        next
      else
        if optional
          next
        else
          return false
        end
      end
    when String
      return true if optional && token_index == tokens.size

      if definitions.key?(name.to_sym)
        sub_handlers = definitions[name.to_sym]
      else
        raise ChronicPain, "Invalid subset #{name} specified"
      end

      sub_handlers.each do |sub_handler|
        return true if sub_handler.match(tokens[token_index..tokens.size], definitions)
      end
    else
      raise ChronicPain, "Invalid match type: #{element.class}"
    end
  end

  return false if token_index != tokens.size
  return true
end