Class: Chronic::Handler

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, handler_method) ⇒ Handler

Returns a new instance of Handler.



490
491
492
493
# File 'lib/chronic/handlers.rb', line 490

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

Instance Attribute Details

#handler_methodObject

Returns the value of attribute handler_method.



488
489
490
# File 'lib/chronic/handlers.rb', line 488

def handler_method
  @handler_method
end

#patternObject

Returns the value of attribute pattern.



488
489
490
# File 'lib/chronic/handlers.rb', line 488

def pattern
  @pattern
end

Instance Method Details

#==(other) ⇒ Object



544
545
546
# File 'lib/chronic/handlers.rb', line 544

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

#constantize(name) ⇒ Object



495
496
497
498
# File 'lib/chronic/handlers.rb', line 495

def constantize(name)
  camel = name.to_s.gsub(/(^|_)(.)/) { $2.upcase }
  ::Chronic.module_eval(camel, __FILE__, __LINE__)
end

#first_not_anchor_index(tokens) ⇒ Object



539
540
541
542
# File 'lib/chronic/handlers.rb', line 539

def first_not_anchor_index(tokens)
  return nil unless index = tokens.reverse.rindex { |e| not e.get_tag(Separator) }
  tokens.length - (index + 1)
end

#last_not_anchor_index(tokens) ⇒ Object



535
536
537
# File 'lib/chronic/handlers.rb', line 535

def last_not_anchor_index(tokens)
  tokens.rindex { |e| not e.get_tag(Separator) } 
end

#match(tokens, definitions) ⇒ Object



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/chronic/handlers.rb', line 500

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].tags.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



528
529
530
531
532
533
# File 'lib/chronic/handlers.rb', line 528

def stripanchors(tokens)
  beg = first_not_anchor_index(tokens)
  fin = last_not_anchor_index(tokens)
  return [] unless beg and fin
  tokens[beg..fin]
end