Class: Workarea::Search::RangeParameter
- Inherits:
-
Object
- Object
- Workarea::Search::RangeParameter
- 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
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Class Method Summary collapse
-
.to_param(value) ⇒ String
Take any range facet value and convert it to its param representation.
Instance Method Summary collapse
-
#initialize(string) ⇒ RangeParameter
constructor
A new instance of RangeParameter.
-
#start ⇒ String
The beginning of the range.
-
#stop ⇒ String
The end of the range.
-
#to_a ⇒ Array
An array form of the filter with start as the first element and stop as the second.
-
#to_filter ⇒ Hash
Returns a hash form of the filter, usable in Elasticsearch query-building.
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
#string ⇒ Object (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.
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
#start ⇒ String
The beginning of the range.
31 32 33 |
# File 'app/queries/workarea/search/range_parameter.rb', line 31 def start to_a[0].present? ? clean(to_a[0]) : '' end |
#stop ⇒ String
The end of the range.
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_a ⇒ Array
An array form of the filter with start as the first element and stop as the second.
48 49 50 |
# File 'app/queries/workarea/search/range_parameter.rb', line 48 def to_a @array ||= string.blank? ? [] : string.split('-') end |
#to_filter ⇒ Hash
Returns a hash form of the filter, usable in Elasticsearch query-building.
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 |