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
- #first_not_anchor_index(tokens) ⇒ Object
-
#initialize(pattern, handler_method) ⇒ Handler
constructor
A new instance of Handler.
- #last_not_anchor_index(tokens) ⇒ Object
- #match(tokens, definitions) ⇒ Object
- #stripanchors(tokens) ⇒ Object
Constructor Details
#initialize(pattern, handler_method) ⇒ Handler
Returns a new instance of Handler.
503 504 505 506 |
# File 'lib/chronic/handlers.rb', line 503 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.
501 502 503 |
# File 'lib/chronic/handlers.rb', line 501 def handler_method @handler_method end |
#pattern ⇒ Object
Returns the value of attribute pattern.
501 502 503 |
# File 'lib/chronic/handlers.rb', line 501 def pattern @pattern end |
Instance Method Details
#==(other) ⇒ Object
555 556 557 |
# File 'lib/chronic/handlers.rb', line 555 def ==(other) self.pattern == other.pattern end |
#constantize(name) ⇒ Object
508 509 510 511 |
# File 'lib/chronic/handlers.rb', line 508 def constantize(name) camel = name.to_s.gsub(/(^|_)(.)/) { $2.upcase } ::Chronic.module_eval(camel, __FILE__, __LINE__) end |
#first_not_anchor_index(tokens) ⇒ Object
551 552 553 |
# File 'lib/chronic/handlers.rb', line 551 def first_not_anchor_index(tokens) tokens.length - tokens.reverse.rindex { |e| not e.get_tag(Separator) } - 1 end |
#last_not_anchor_index(tokens) ⇒ Object
547 548 549 |
# File 'lib/chronic/handlers.rb', line 547 def last_not_anchor_index(tokens) tokens.rindex { |e| not e.get_tag(Separator) } end |
#match(tokens, definitions) ⇒ Object
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/chronic/handlers.rb', line 513 def match(tokens, definitions) tokens = stripanchors(tokens) 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 |
#stripanchors(tokens) ⇒ Object
541 542 543 544 545 |
# File 'lib/chronic/handlers.rb', line 541 def stripanchors(tokens) fin = last_not_anchor_index(tokens) beg = first_not_anchor_index(tokens) tokens[beg..fin] end |