Class: Brita::Filter
- Inherits:
-
Object
- Object
- Brita::Filter
- 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
-
#custom_validate ⇒ Object
readonly
Returns the value of attribute custom_validate.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#parameter ⇒ Object
readonly
Returns the value of attribute parameter.
-
#scope_params ⇒ Object
readonly
Returns the value of attribute scope_params.
Instance Method Summary collapse
-
#always_active? ⇒ Boolean
rubocop:enable Lint/UnusedMethodArgument.
-
#apply!(collection, value:, active_sorts_hash:, params: {}) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
-
#initialize(param, type, internal_name, default, custom_validate = nil, scope_params = []) ⇒ Filter
constructor
A new instance of Filter.
- #param ⇒ Object
- #type ⇒ Object
- #type_validator ⇒ Object
- #validation(_sort) ⇒ Object
- #validation_field ⇒ Object
Constructor Details
#initialize(param, type, internal_name, default, custom_validate = nil, scope_params = []) ⇒ Filter
Returns a new instance of Filter.
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_validate ⇒ Object (readonly)
Returns the value of attribute custom_validate.
5 6 7 |
# File 'lib/brita/filter.rb', line 5 def custom_validate @custom_validate end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
5 6 7 |
# File 'lib/brita/filter.rb', line 5 def default @default end |
#parameter ⇒ Object (readonly)
Returns the value of attribute parameter.
5 6 7 |
# File 'lib/brita/filter.rb', line 5 def parameter @parameter end |
#scope_params ⇒ Object (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
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 |
#param ⇒ Object
48 49 50 |
# File 'lib/brita/filter.rb', line 48 def param parameter.param end |
#type ⇒ Object
44 45 46 |
# File 'lib/brita/filter.rb', line 44 def type parameter.type end |
#type_validator ⇒ Object
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_field ⇒ Object
36 37 38 |
# File 'lib/brita/filter.rb', line 36 def validation_field parameter.param end |