Class: ActiveInteraction::HashFilter

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

Instance Attribute Summary

Attributes inherited from Filter

#filters, #name, #options

Instance Method Summary collapse

Methods inherited from Filter

#accepts_grouped_inputs?, #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 (private)

rubocop:disable Style/MissingRespondToMissing



79
80
81
82
83
84
85
86
87
# File 'lib/active_interaction/filters/hash_filter.rb', line 79

def method_missing(*args, &block)
  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

#process(value, context) ⇒ Object

rubocop:disable Metrics/AbcSize



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/active_interaction/filters/hash_filter.rb', line 28

def process(value, context) # rubocop:disable Metrics/AbcSize
  input = super

  return HashInput.new(self, value: input.value, error: input.errors.first) if input.errors.first
  return HashInput.new(self, value: default(context), error: input.errors.first) if input.value.nil?

  value = strip? ? HashWithIndifferentAccess.new : input.value
  error = nil
  children = {}

  filters.each do |name, filter|
    if filter.options[:default].is_a?(Proc) && !options[:default].is_a?(Proc)
      raise InvalidDefaultError, "#{self.name}: must use a lazy default if any nested filter uses a lazy default"
    end

    filter.process(input.value[name], context).tap do |result|
      value[name] = result.value
      children[name.to_sym] = result
    end
  end

  HashInput.new(self, value: value, error: error, children: children)
end