Class: ActiveInteraction::HashFilter

Inherits:
Filter
  • Object
show all
Includes:
Missable
Defined in:
lib/active_interaction/filters/hash_filter.rb

Overview

Since:

  • 1.0.0

Instance Attribute Summary

Attributes inherited from Filter

#filters, #name, #options

Instance Method Summary collapse

Methods inherited from Filter

#clean, #database_column_type, #default, #default?, #desc, factory, #initialize

Constructor Details

This class inherits a constructor from ActiveInteraction::Filter

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object

rubocop:disable Style/MethodMissing

Since:

  • 1.0.0



43
44
45
46
47
48
49
50
51
# File 'lib/active_interaction/filters/hash_filter.rb', line 43

def method_missing(*args, &block) # rubocop:disable Style/MethodMissing
  super(*args) do |klass, names, options|
    raise InvalidFilterError, 'missing attribute name' if names.empty?

    names.each do |name|
      filters[name] = klass.new(name, options, &block)
    end
  end
end

Instance Method Details

#cast(value, context) ⇒ Object

Since:

  • 1.0.0



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_interaction/filters/hash_filter.rb', line 29

def cast(value, context)
  case value
  when Hash
    value = value.with_indifferent_access
    initial = strip? ? ActiveSupport::HashWithIndifferentAccess.new : value

    filters.each_with_object(initial) do |(name, filter), h|
      clean_value(h, name.to_s, filter, value, context)
    end
  else
    super
  end
end