Class: Brita::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/brita/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 = []) ⇒ Filter

Returns a new instance of Filter.

Raises:

  • (ArgumentError)


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

def initialize(param, type, internal_name, default, custom_validate = nil, scope_params = [])
  @parameter = Parameter.new(param, type, internal_name)
  @default = default
  @custom_validate = custom_validate
  @scope_params = scope_params
  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/brita/filter.rb', line 5

def custom_validate
  @custom_validate
end

#defaultObject (readonly)

Returns the value of attribute default.



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

def default
  @default
end

#parameterObject (readonly)

Returns the value of attribute parameter.



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

def parameter
  @parameter
end

#scope_paramsObject (readonly)

Returns the value of attribute scope_params.



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

def scope_params
  @scope_params
end

Instance Method Details

#always_active?Boolean

rubocop:enable Lint/UnusedMethodArgument

Returns:

  • (Boolean)


32
33
34
# File 'lib/brita/filter.rb', line 32

def always_active?
  false
end

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

rubocop:disable Lint/UnusedMethodArgument



21
22
23
24
25
26
27
28
29
# File 'lib/brita/filter.rb', line 21

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

#paramObject



48
49
50
# File 'lib/brita/filter.rb', line 48

def param
  parameter.param
end

#typeObject



44
45
46
# File 'lib/brita/filter.rb', line 44

def type
  parameter.type
end

#type_validatorObject



40
41
42
# File 'lib/brita/filter.rb', line 40

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

#validation(_sort) ⇒ Object



16
17
18
# File 'lib/brita/filter.rb', line 16

def validation(_sort)
  type_validator.validate
end

#validation_fieldObject



36
37
38
# File 'lib/brita/filter.rb', line 36

def validation_field
  parameter.param
end