Sha256: ef24a94f7aa4aab69bb6c536689706df8f17243798a54d1d4a65e74cba34bf08

Contents?: true

Size: 1.59 KB

Versions: 62

Compression:

Stored size: 1.59 KB

Contents

module Workarea
  module Search
    # This class represents a range filter coming in from a query string.
    # It parses this query string into a usable data structure.
    #
    class RangeParameter
      attr_reader :string

      # Take any range facet value and convert it to its param representation.
      #
      # @param [Object]
      # @return [String]
      #
      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

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

      # The beginning of the range.
      #
      # @return [String]
      #
      def start
        to_a[0].present? ? clean(to_a[0]) : ''
      end

      # The end of the range.
      #
      # @return [String]
      #
      def stop
        to_a[1].present? ? clean(to_a[1]) : ''
      end

      # An array form of the filter with start as the first
      # element and stop as the second.
      #
      # @return [Array]
      #
      def to_a
        @array ||= string.blank? ? [] : string.split('-')
      end

      # Returns a hash form of the filter, usable
      # in Elasticsearch query-building.
      #
      # @return [Hash]
      #
      def to_filter
        result = {}
        result[:gte] = start if start.present? && start != '*'
        result[:lt] = stop if stop.present? && stop != '*'
        result
      end

      private

      def clean(string)
        string.gsub(/['"]/, '')
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.4.27 app/queries/workarea/search/range_parameter.rb
workarea-core-3.5.4 app/queries/workarea/search/range_parameter.rb
workarea-core-3.4.26 app/queries/workarea/search/range_parameter.rb
workarea-core-3.5.3 app/queries/workarea/search/range_parameter.rb
workarea-core-3.4.25 app/queries/workarea/search/range_parameter.rb
workarea-core-3.5.2 app/queries/workarea/search/range_parameter.rb
workarea-core-3.4.24 app/queries/workarea/search/range_parameter.rb
workarea-core-3.5.1 app/queries/workarea/search/range_parameter.rb
workarea-core-3.4.23 app/queries/workarea/search/range_parameter.rb
workarea-core-3.4.22 app/queries/workarea/search/range_parameter.rb
workarea-core-3.5.0 app/queries/workarea/search/range_parameter.rb
workarea-core-3.4.21 app/queries/workarea/search/range_parameter.rb
workarea-core-3.5.0.beta.1 app/queries/workarea/search/range_parameter.rb
workarea-core-3.4.20 app/queries/workarea/search/range_parameter.rb
workarea-core-3.4.19 app/queries/workarea/search/range_parameter.rb
workarea-core-3.4.18 app/queries/workarea/search/range_parameter.rb
workarea-core-3.4.17 app/queries/workarea/search/range_parameter.rb
workarea-core-3.4.16 app/queries/workarea/search/range_parameter.rb
workarea-core-3.4.15 app/queries/workarea/search/range_parameter.rb
workarea-core-3.4.14 app/queries/workarea/search/range_parameter.rb