chronic icon indicating copy to clipboard operation
chronic copied to clipboard

Feature Request: option to specify which handlers to use

Open pietdaniel opened this issue 10 years ago • 3 comments
trafficstars

Handlers and Needed Use Case

A lot of the handlers are amazing but without the ability to fine tune which handlers I want to use I am unable to use this library for my use case. I only care for the date specific handlers to be active such that the following,

Chronic.parse("17 april 89")  => 1969-04-17 12:00:00 -0500

will parse as expected. Except I wish to turn of the 'simple' and 'complex' handlers such that:

Chronic.parse("tomorrow") => nil

Possible implementation

This could be done through the option interface, either via definitions

Chronic.parse("tomorrow", :definitions => :date)

or view handler specification

Chronic.parse("tomorrow", :handlers => [:handle_m_d])

pietdaniel avatar Feb 16 '15 18:02 pietdaniel

I've been thinking about such feature for next version as I also find this needed and useful, but I haven't decided yet about best implementation.

davispuh avatar Feb 17 '15 16:02 davispuh

At the moment I've constructed this monkey patch

   def self.get_parser
     # need to monkey patch out the unneccessary parse definitions
     definition_grabber = Chronic::Parser.new
     new_definitions = {}
     new_definitions[:date] = definition_grabber.definitions[:date]
     new_definitions[:endian] = definition_grabber.definitions[:endian]
     new_definitions[:anchor] = {}
     new_definitions[:arrow] = {}
     new_definitions[:narrow] = {}
     new_definitions[:time] = {}

     Chronic::Parser.send(:define_method, "definitions") do |*options|
       new_definitions
     end

     Chronic::Parser.new
   end

but knowing that the feature is needed I will probably submit a PR. The approach I foresee implementing is allowing the definitions method to grab definitions from SpanDictionary based upon flags set within the options variable. Something like

Chronic.parse("tomorrow", :definitions => [:date, :endian])

which would provide results similar to the monkey patch

EDIT: (did not mean to close this issue)

pietdaniel avatar Feb 17 '15 16:02 pietdaniel

I don't recommend working much on current master branch because those changes will have to be reworked for next version, I've rewrite branch in progress, see #278 and definitions are changed quite radically there... but if you need this change soon then just go for it.

davispuh avatar Feb 17 '15 19:02 davispuh