class SearchLingo::Parsers::DateParser
DateParser
is an example parser which handles dates that adhere to the MDY
format used in the US. It uses `SearchLingo::Parsers::MDY.parse` to parse the date. It handles simple dates as well as closed and open-ended date ranges.
Examples of single dates are 7/14, 7/14/17, and 7/14/2017. Examples of closed date ranges are 1/1-6/30 and 7/1/16-6/30/18. Examples of open date ranges are -6/30 and 7/1/17-.
Attributes
Public Class Methods
Instantiates a new DateParser
object.
The required argument column
should be an Arel attribute.
If present, the optional argument modifier
will be used as the token operator which precedes the date term.
If a block is provided, it will be used to append additional methods to the filter chain. (This is useful for static methods that must be appended to the filter chain independent of the content of the token, for example, if you need to join another table.)
DateParser.new
Model.arel_table DateParser.new
Model.arel_table, modifier: 'contract' DateParser.new
Model.arel_table do |chain|
chain.joins(:relation)
end
# File lib/search_lingo/parsers/date_parser.rb, line 39 def initialize(column, modifier: nil, &block) @column = column @prefix = /#{modifier}:[[:space:]]*/ if modifier @append = block_given? ? block : ->(chain) { chain } end
Public Instance Methods
Attempts to parse token
as a single date, closed date range, or open date range. If parsing succeeds, the parser sends where to chain
with the appropriate Arel node and returns the result.
# File lib/search_lingo/parsers/date_parser.rb, line 49 def call(token, chain) catch :halt do parse_single_date token, chain parse_date_range token, chain parse_lte_date token, chain parse_gte_date token, chain end end