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.



374
375
376
377
# File 'lib/chronic/handlers.rb', line 374

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

Instance Attribute Details

#handler_methodObject

Returns the value of attribute handler_method.



372
373
374
# File 'lib/chronic/handlers.rb', line 372

def handler_method
  @handler_method
end

#patternObject

Returns the value of attribute pattern.



372
373
374
# File 'lib/chronic/handlers.rb', line 372

def pattern
  @pattern
end

Instance Method Details

#constantize(name) ⇒ Object



379
380
381
382
# File 'lib/chronic/handlers.rb', line 379

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

#match(tokens, definitions) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/chronic/handlers.rb', line 384

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