Class: Sift::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/sift/filter.rb

Overview

Filter describes the way a parameter maps to a database column and the type information helpful for validating input.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param, type, internal_name, default, custom_validate = nil, scope_params = [], tap = ->(value, _params) { value }) ⇒ Filter

Returns a new instance of Filter.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
# File 'lib/sift/filter.rb', line 7

def initialize(param, type, internal_name, default, custom_validate = nil, scope_params = [], tap = ->(value, _params) { value })
  @parameter = Parameter.new(param, type, internal_name)
  @default = default
  @custom_validate = custom_validate
  @scope_params = scope_params
  @tap = tap
  raise ArgumentError, "scope_params must be an array of symbols" unless valid_scope_params?(scope_params)
  raise "unknown filter type: #{type}" unless type_validator.valid_type?
end

Instance Attribute Details

#custom_validateObject (readonly)

Returns the value of attribute custom_validate.



5
6
7
# File 'lib/sift/filter.rb', line 5

def custom_validate
  @custom_validate
end

#defaultObject (readonly)

Returns the value of attribute default.



5
6
7
# File 'lib/sift/filter.rb', line 5

def default
  @default
end

#parameterObject (readonly)

Returns the value of attribute parameter.



5
6
7
# File 'lib/sift/filter.rb', line 5

def parameter
  @parameter
end

#scope_paramsObject (readonly)

Returns the value of attribute scope_params.



5
6
7
# File 'lib/sift/filter.rb', line 5

def scope_params
  @scope_params
end

Instance Method Details

#always_active?Boolean

rubocop:enable Lint/UnusedMethodArgument

Returns:

  • (Boolean)


35
36
37
# File 'lib/sift/filter.rb', line 35

def always_active?
  false
end

#apply!(collection, value:, active_sorts_hash:, params: {}) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sift/filter.rb', line 22

def apply!(collection, value:, active_sorts_hash:, params: {})
  if not_processable?(value)
    collection
  elsif should_apply_default?(value)
    default.call(collection)
  else
    parameterized_values = parameterize(value)
    processed_values = @tap.present? ? @tap.call(parameterized_values, params) : parameterized_values
    handler.call(collection, processed_values, params, scope_params)
  end
end

#paramObject



51
52
53
# File 'lib/sift/filter.rb', line 51

def param
  parameter.param
end

#typeObject



47
48
49
# File 'lib/sift/filter.rb', line 47

def type
  parameter.type
end

#type_validatorObject



43
44
45
# File 'lib/sift/filter.rb', line 43

def type_validator
  @type_validator ||= Sift::TypeValidator.new(param, type)
end

#validation(_sort) ⇒ Object



17
18
19
# File 'lib/sift/filter.rb', line 17

def validation(_sort)
  type_validator.validate
end

#validation_fieldObject



39
40
41
# File 'lib/sift/filter.rb', line 39

def validation_field
  parameter.param
end