Class: Chronic::Handler
- Inherits:
-
Object
- Object
- Chronic::Handler
- Defined in:
- lib/chronic/handler.rb
Instance Attribute Summary collapse
-
#handler_method ⇒ Object
readonly
Returns the value of attribute handler_method.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Instance Method Summary collapse
-
#==(other) ⇒ Object
other - The other Handler object to compare.
-
#initialize(pattern, handler_method) ⇒ Handler
constructor
pattern - An Array of patterns to match tokens against.
- #invoke(type, tokens, parser, options) ⇒ Object
-
#match(tokens, definitions) ⇒ Object
tokens - An Array of tokens to process.
Constructor Details
#initialize(pattern, handler_method) ⇒ Handler
pattern - An Array of patterns to match tokens against. handler_method - A Symbol representing the method to be invoked
when a pattern matches.
11 12 13 14 |
# File 'lib/chronic/handler.rb', line 11 def initialize(pattern, handler_method) @pattern = pattern @handler_method = handler_method end |
Instance Attribute Details
#handler_method ⇒ Object (readonly)
Returns the value of attribute handler_method.
6 7 8 |
# File 'lib/chronic/handler.rb', line 6 def handler_method @handler_method end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
4 5 6 |
# File 'lib/chronic/handler.rb', line 4 def pattern @pattern end |
Instance Method Details
#==(other) ⇒ Object
other - The other Handler object to compare.
Returns true if these Handlers match.
73 74 75 |
# File 'lib/chronic/handler.rb', line 73 def ==(other) @pattern == other.pattern end |
#invoke(type, tokens, parser, options) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/chronic/handler.rb', line 61 def invoke(type, tokens, parser, ) if Chronic.debug puts "-#{type}" puts "Handler: #{@handler_method}" end parser.send(@handler_method, tokens, ) end |
#match(tokens, definitions) ⇒ Object
tokens - An Array of tokens to process. definitions - A Hash of definitions to check against.
Returns true if a match is found.
20 21 22 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 |
# File 'lib/chronic/handler.rb', line 20 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 (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 "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 "Invalid match type: #{element.class}" end end return false if token_index != tokens.size return true end |