Class: Chronic::Handler
- Inherits:
-
Object
- Object
- Chronic::Handler
- Defined in:
- lib/chronic/handlers.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#handler_method ⇒ Object
Returns the value of attribute handler_method.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #constantize(name) ⇒ Object
-
#initialize(pattern, handler_method) ⇒ Handler
constructor
A new instance of Handler.
- #match(tokens, definitions) ⇒ Object
Constructor Details
#initialize(pattern, handler_method) ⇒ Handler
Returns a new instance of Handler.
482 483 484 485 |
# File 'lib/chronic/handlers.rb', line 482 def initialize(pattern, handler_method) @pattern = pattern @handler_method = handler_method end |
Instance Attribute Details
#handler_method ⇒ Object
Returns the value of attribute handler_method.
480 481 482 |
# File 'lib/chronic/handlers.rb', line 480 def handler_method @handler_method end |
#pattern ⇒ Object
Returns the value of attribute pattern.
480 481 482 |
# File 'lib/chronic/handlers.rb', line 480 def pattern @pattern end |
Instance Method Details
#==(other) ⇒ Object
519 520 521 |
# File 'lib/chronic/handlers.rb', line 519 def ==(other) self.pattern == other.pattern end |
#constantize(name) ⇒ Object
487 488 489 490 |
# File 'lib/chronic/handlers.rb', line 487 def constantize(name) camel = name.to_s.gsub(/(^|_)(.)/) { $2.upcase } ::Chronic.module_eval(camel, __FILE__, __LINE__) end |
#match(tokens, definitions) ⇒ Object
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 |
# File 'lib/chronic/handlers.rb', line 492 def match(tokens, definitions) token_index = 0 @pattern.each do |element| name = element.to_s optional = name.reverse[0..0] == '?' name = name.chop if optional if element.instance_of? Symbol klass = constantize(name) match = tokens[token_index] && !tokens[token_index]..select { |o| o.kind_of?(klass) }.empty? return false if !match && !optional (token_index += 1; next) if match next if !match && optional elsif element.instance_of? String return true if optional && token_index == tokens.size sub_handlers = definitions[name.intern] || raise(ChronicPain, "Invalid subset #{name} specified") sub_handlers.each do |sub_handler| return true if sub_handler.match(tokens[token_index..tokens.size], definitions) end return false else raise(ChronicPain, "Invalid match type: #{element.class}") end end return false if token_index != tokens.size return true end |