Class: Workarea::Search::RangeParameter

Inherits:
Object
  • Object
show all
Defined in:
app/queries/workarea/search/range_parameter.rb

Overview

This class represents a range filter coming in from a query string. It parses this query string into a usable data structure.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ RangeParameter

Returns a new instance of RangeParameter.



23
24
25
# File 'app/queries/workarea/search/range_parameter.rb', line 23

def initialize(string)
  @string = string.to_s
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



7
8
9
# File 'app/queries/workarea/search/range_parameter.rb', line 7

def string
  @string
end

Class Method Details

.to_param(value) ⇒ String

Take any range facet value and convert it to its param representation.

Parameters:

  • (Object)

Returns:



14
15
16
17
18
19
20
21
# File 'app/queries/workarea/search/range_parameter.rb', line 14

def self.to_param(value)
  if value.respond_to?(:to_h)
    value = value.to_h.with_indifferent_access
    "#{value[:from].presence || '*'}-#{value[:to].presence || '*'}"
  else
    value.to_s
  end
end

Instance Method Details

#startString

The beginning of the range.

Returns:



31
32
33
# File 'app/queries/workarea/search/range_parameter.rb', line 31

def start
  to_a[0].present? ? clean(to_a[0]) : ''
end

#stopString

The end of the range.

Returns:



39
40
41
# File 'app/queries/workarea/search/range_parameter.rb', line 39

def stop
  to_a[1].present? ? clean(to_a[1]) : ''
end

#to_aArray

An array form of the filter with start as the first element and stop as the second.

Returns:

  • (Array)


48
49
50
# File 'app/queries/workarea/search/range_parameter.rb', line 48

def to_a
  @array ||= string.blank? ? [] : string.split('-')
end

#to_filterHash

Returns a hash form of the filter, usable in Elasticsearch query-building.

Returns:

  • (Hash)


57
58
59
60
61
62
# File 'app/queries/workarea/search/range_parameter.rb', line 57

def to_filter
  result = {}
  result[:gte] = start if start.present? && start != '*'
  result[:lt] = stop if stop.present? && stop != '*'
  result
end