Class: Sift::Filter
- Inherits:
-
Object
- Object
- Sift::Filter
- 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
-
#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 = [], tap = ->(value, _params) { value }) ⇒ 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 = [], tap = ->(value, _params) { value }) ⇒ Filter
Returns a new instance of Filter.
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_validate ⇒ Object (readonly)
Returns the value of attribute custom_validate.
5 6 7 |
# File 'lib/sift/filter.rb', line 5 def custom_validate @custom_validate end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
5 6 7 |
# File 'lib/sift/filter.rb', line 5 def default @default end |
#parameter ⇒ Object (readonly)
Returns the value of attribute parameter.
5 6 7 |
# File 'lib/sift/filter.rb', line 5 def parameter @parameter end |
#scope_params ⇒ Object (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
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 |
#param ⇒ Object
51 52 53 |
# File 'lib/sift/filter.rb', line 51 def param parameter.param end |
#type ⇒ Object
47 48 49 |
# File 'lib/sift/filter.rb', line 47 def type parameter.type end |
#type_validator ⇒ Object
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_field ⇒ Object
39 40 41 |
# File 'lib/sift/filter.rb', line 39 def validation_field parameter.param end |