Class: Caoutsearch::Filter::Date

Inherits:
Base
  • Object
show all
Defined in:
lib/caoutsearch/filter/date.rb

Constant Summary collapse

RANGE_OPERATORS =
{
  "less_than" => "lt",
  "less_than_or_equal" => "lte",
  "greater_than" => "gt",
  "greater_than_or_equal" => "gte"
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#key, #options, #original_value, #type

Instance Method Summary collapse

Methods inherited from Base

#as_json, call, #initialize, #value

Constructor Details

This class inherits a constructor from Caoutsearch::Filter::Base

Instance Method Details

#build_range_query(input) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/caoutsearch/filter/date.rb', line 43

def build_range_query(input)
  lower_bound = input.is_a?(::Range) ? input.begin : input.first
  upper_bound = input.is_a?(::Range) ? input.end : input.last
  return unless upper_bound || lower_bound

  query = {range: {key => {}}}
  query[:range][key][:gte] = cast_value(lower_bound) if lower_bound

  if upper_bound
    if input.is_a?(::Range) && input.exclude_end?
      query[:range][key][:lt] = cast_value(upper_bound)
    else
      query[:range][key][:lte] = cast_value(upper_bound)
    end
  end

  query
end

#cast_date(value, unit) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/caoutsearch/filter/date.rb', line 83

def cast_date(value, unit)
  if value.is_a?(Numeric) && unit
    case unit
    when "day" then value = value.days.ago.to_date
    when "week" then value = value.weeks.ago.to_date
    when "month" then value = value.months.ago.to_date
    when "year", nil then value = value.years.ago.to_date
    else
      raise ArgumentError, "unknown unit #{unit.inspect} in #{value.inspect}"
    end
  elsif value.is_a?(ActiveSupport::Duration)
    value = value.ago.to_date
  end

  cast_value(value)
end

#cast_operator(original_operator) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/caoutsearch/filter/date.rb', line 69

def cast_operator(original_operator)
  operator = original_operator.to_s
  return original_operator if RANGE_OPERATORS.value?(operator)

  operator = RANGE_OPERATORS[operator]
  if operator.nil?
    raise ArgumentError, "unknown operator #{original_operator.inspect}"
  elsif original_operator.is_a?(Symbol)
    operator.to_sym
  else
    operator
  end
end

#filterObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/caoutsearch/filter/date.rb', line 6

def filter
  original_values.map do |input|
    case input
    when true
      {exists: {field: key}}
    when false
      {bool: {must_not: {exists: {field: key}}}}
    when ::Date
      {range: {key => {gte: input.as_json, lte: input.as_json}}}
    when ::String
      case input
      when /\A\.\.(.+)\Z/
        build_range_query(..$1)
      when /\A(.+)\.\.\Z/
        build_range_query($1..)
      when /\A(.+)\.\.(.+)\Z/
        build_range_query($1..$2)
      else
        {range: {key => {gte: input, lte: input}}}
      end
    when ::Range, ::Array
      build_range_query(input)
    when ::Hash
      case input
      in { between: dates }
        build_range_query(dates)
      else
        parameters = input.to_h do |operator, value|
          [cast_operator(operator), cast_value(value)]
        end

        {range: {key => parameters}}
      end
    end
  end
end