Sha256: 14f6f19d3f57a1ccc458c3199793c8fa7e17893aa6ff82fbd5373902932c2825

Contents?: true

Size: 897 Bytes

Versions: 3

Compression:

Stored size: 897 Bytes

Contents

# frozen_string_literal: true

module QueryFilter
  module Rules
    class SplitterRange < Scope
      class RangeParam
        SPLITTER = /[;-]/.freeze

        def initialize(value)
          @value = value
        end

        def valid?
          @value.present? && items.size == 2
        end

        def range
          ::Range.new(*items)
        end

        def items
          @items ||= @value.split(SPLITTER).map(&:strip).map(&:to_i)
        end
      end

      def name
        'splitter_range'
      end

      def valid?(values)
        period = build_period_from_params(values)
        !period.nil?
      end

      def normalize_params(values)
        build_period_from_params(values)
      end

      protected

      def build_period_from_params(values)
        param = RangeParam.new(values[key])
        return unless param.valid?

        param
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
query_filter-0.2.3 lib/query_filter/rules/splitter_range.rb
query_filter-0.2.2 lib/query_filter/rules/splitter_range.rb
query_filter-0.2.0 lib/query_filter/rules/splitter_range.rb