lib/chronic/handler.rb in chronic-mmlac-0.6.4.2 vs lib/chronic/handler.rb in chronic-mmlac-0.10.2.1
- old
+ new
@@ -1,79 +1,86 @@
module Chronic
class Handler
- # @return [Array] A list of patterns
attr_reader :pattern
- # @return [Symbol] The method which handles this list of patterns.
- # This method should exist inside the {Handlers} module
attr_reader :handler_method
- # @param [Array] pattern A list of patterns to match tokens against
- # @param [Symbol] handler_method The method to be invoked when patterns
- # are matched. This method should exist inside the {Handlers} module
+ # pattern - An Array of patterns to match tokens against.
+ # handler_method - A Symbol representing the method to be invoked
+ # when a pattern matches.
def initialize(pattern, handler_method)
@pattern = pattern
@handler_method = handler_method
end
- # @param [Array] tokens
- # @param [Hash] definitions
- # @return [Boolean]
- # @see Chronic.tokens_to_span
+ # tokens - An Array of tokens to process.
+ # definitions - A Hash of definitions to check against.
+ #
+ # Returns true if a match is found.
def match(tokens, definitions)
token_index = 0
+ @pattern.each do |elements|
+ was_optional = false
+ elements = [elements] unless elements.is_a?(Array)
- @pattern.each do |element|
- name = element.to_s
- optional = name[-1, 1] == '?'
- name = name.chop if optional
+ elements.each_index do |i|
+ name = elements[i].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
+ case elements[i]
+ when Symbol
+ if tags_match?(name, tokens, token_index)
+ token_index += 1
+ break
else
- return false
+ if optional
+ was_optional = true
+ next
+ elsif i + 1 < elements.count
+ next
+ else
+ return false unless was_optional
+ end
end
- end
- when String
- return true if optional && token_index == tokens.size
- if definitions.key?(name.to_sym)
- sub_handlers = definitions[name.to_sym]
+ 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 ChronicPain, "Invalid subset #{name} specified"
+ raise "Invalid match type: #{elements[i].class}"
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
- def invoke(type, tokens, options)
+ def invoke(type, tokens, parser, options)
if Chronic.debug
puts "-#{type}"
puts "Handler: #{@handler_method}"
end
- Handlers.send(@handler_method, tokens, options)
+ parser.send(@handler_method, tokens, options)
end
- # @param [Handler] The handler to compare
- # @return [Boolean] True if these handlers match
+ # other - The other Handler object to compare.
+ #
+ # Returns true if these Handlers match.
def ==(other)
@pattern == other.pattern
end
private
@@ -85,6 +92,6 @@
!tokens[token_index].tags.select { |o| o.kind_of?(klass) }.empty?
end
end
end
-end
\ No newline at end of file
+end