Class: SearchLingo::Parsers::DateParser

Inherits:
Object
  • Object
show all
Includes:
MDY
Defined in:
lib/search_lingo/parsers/date_parser.rb

Constant Summary

Constants included from MDY

MDY::US_DATE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MDY

parse

Constructor Details

#initialize(column, modifier: nil) ⇒ DateParser

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 operator which precedes the date term.

DateParser.new Booking.arel_table DateParser.new Contract.arel_table, modifier: ‘contract’

[View source]

18
19
20
21
# File 'lib/search_lingo/parsers/date_parser.rb', line 18

def initialize(column, modifier: nil)
  @column = column
  @prefix = %r{#{modifier}:[[:space:]]*} if modifier
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.


23
24
25
# File 'lib/search_lingo/parsers/date_parser.rb', line 23

def column
  @column
end

#prefixObject (readonly)

Returns the value of attribute prefix.


23
24
25
# File 'lib/search_lingo/parsers/date_parser.rb', line 23

def prefix
  @prefix
end

Instance Method Details

#call(token) ⇒ Object

Attempts to parse the token as a date, closed date range, or open date range.

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

[View source]

32
33
34
35
36
37
# File 'lib/search_lingo/parsers/date_parser.rb', line 32

def call(token)
  parse_single_date(token)  ||
    parse_date_range(token) ||
    parse_lte_date(token)   ||
    parse_gte_date(token)
end

#inspectObject

:nodoc:

[View source]

39
40
41
42
# File 'lib/search_lingo/parsers/date_parser.rb', line 39

def inspect # :nodoc:
  '#<%s:0x%x @prefix=%s @column=%s>' %
    [self.class, object_id << 1, prefix.inspect, column.inspect]
end