Class: Objective::Filters::HashFilter

Inherits:
Objective::Filter show all
Defined in:
lib/objective/filters/hash_filter.rb

Constant Summary collapse

Options =
OpenStruct.new(
  nils: Objective::DENY,
  invalid: Objective::DENY,
  strict: false
)

Instance Attribute Summary

Attributes inherited from Objective::Filter

#key, #sub_filters

Instance Method Summary collapse

Methods inherited from Objective::Filter

#coerce, #default, #default?, #dup, #feed_empty, #feed_invalid, #feed_nil, #feed_result, #handle_errors, inherited, #initialize, #options, #sub_filters_hash, #validate

Constructor Details

This class inherits a constructor from Objective::Filter

Instance Method Details

#coerce_error(coerced) ⇒ Object



39
40
41
# File 'lib/objective/filters/hash_filter.rb', line 39

def coerce_error(coerced)
  return :hash unless coerced.is_a?(Hash)
end

#feed(raw) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/objective/filters/hash_filter.rb', line 12

def feed(raw)
  result = super(raw)
  return result if result.errors || result.inputs.nil?

  errors = Objective::Errors::ErrorHash.new
  inputs = {}

  data = result.coerced
  sub_filters_hash.each_pair do |key, key_filter|
    datum = data[key]
    key_filter_result = key_filter.feed(datum)

    sub_data = key_filter_result.inputs
    sub_error = key_filter_result.errors

    if sub_error.nil?
      inputs[key] = sub_data
    else
      errors[key] = key_filter.handle_errors(sub_error)
    end
  end

  result.inputs = inputs
  result.errors = errors.empty? ? nil : errors
  result
end