Sha256: 1905b1e769da275ac5a423b26cdb928f4da36a4145d7b23fe5a56ec97041fcab

Contents?: true

Size: 782 Bytes

Versions: 2

Compression:

Stored size: 782 Bytes

Contents

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

      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
      'spliter_range'.freeze
    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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
query_filter-0.1.1 lib/query_filter/rules/splitter_range.rb
query_filter-0.1.0 lib/query_filter/rules/splitter_range.rb