Class: Paraphrase::ParamsFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/paraphrase/params_filter.rb

Overview

ParamsFilter is responsible for processing the query params the Query object was initialized with.

In the following order, it:

  1. Removes all keys not mapped to a model scope
  2. Pre-processes the query param if a pre-processor is defined
  3. Recursively removes blank values from the value
  4. Removes the param if the pre-processed, scrubbed value is blank?

Each Query subclass has its own ParamsFilter subclass defined on inheritance that can be customized to pre-process query params. The class can be re-opened inside the Query class definition or by calling the param class method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unfiltered_params, keys) ⇒ ParamsFilter

Returns a new instance of ParamsFilter.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/paraphrase/params_filter.rb', line 22

def initialize(unfiltered_params, keys)
  @params = unfiltered_params.with_indifferent_access.slice(*keys)

  @result = keys.inject(HashWithIndifferentAccess.new) do |result, key|
    value = respond_to?(key) ? send(key) : @params[key]
    value = scrub(value)

    if value.present?
      result[key] = value
    end

    result
  end
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



20
21
22
# File 'lib/paraphrase/params_filter.rb', line 20

def params
  @params
end

#resultObject (readonly)

Returns the value of attribute result.



20
21
22
# File 'lib/paraphrase/params_filter.rb', line 20

def result
  @result
end