Class: Elasticband::Filter::Range

Inherits:
Elasticband::Filter show all
Defined in:
lib/elasticband/filter/range.rb

Constant Summary collapse

RANGES =
%i(gt gteq lt lteq).freeze

Constants inherited from Elasticband::Filter

PARSE_FILTERS

Instance Attribute Summary collapse

Attributes inherited from Elasticband::Filter

#options

Instance Method Summary collapse

Methods inherited from Elasticband::Filter

parse, #parse

Constructor Details

#initialize(field, ranges) ⇒ Range

Returns a new instance of Range.



8
9
10
11
# File 'lib/elasticband/filter/range.rb', line 8

def initialize(field, ranges)
  self.field = field.to_sym
  self.ranges = permitted_ranges(ranges)
end

Instance Attribute Details

#fieldObject

Returns the value of attribute field.



4
5
6
# File 'lib/elasticband/filter/range.rb', line 4

def field
  @field
end

#rangesObject

Returns the value of attribute ranges.



4
5
6
# File 'lib/elasticband/filter/range.rb', line 4

def ranges
  @ranges
end

Instance Method Details

#parsed_rangesObject



21
22
23
24
25
26
27
28
# File 'lib/elasticband/filter/range.rb', line 21

def parsed_ranges
  translated_range = ranges.dup

  translated_range[:gte] = translated_range.delete(:gteq) if translated_range.key?(:gteq)
  translated_range[:lte] = translated_range.delete(:lteq) if translated_range.key?(:lteq)

  translated_range
end

#permitted_ranges(ranges) ⇒ Object



17
18
19
# File 'lib/elasticband/filter/range.rb', line 17

def permitted_ranges(ranges)
  ranges.keep_if { |key, _| RANGES.include?(key) }
end

#to_hObject



13
14
15
# File 'lib/elasticband/filter/range.rb', line 13

def to_h
  { range: { field => parsed_ranges } }
end