Class: Objective::Filters::ArrayFilter
Constant Summary
collapse
- Options =
OpenStruct.new(
none: Objective::DENY,
nils: Objective::DENY,
invalid: Objective::DENY,
strict: false,
wrap: false
)
Instance Attribute Summary
#key, #sub_filters
Instance Method Summary
collapse
#default, #default?, #dup, #feed_empty, #feed_invalid, #feed_nil, #feed_none, #feed_result, filter_name, #handle_errors, inherited, #initialize, #options, #sub_filters_hash, #validate
Instance Method Details
#coerce(raw) ⇒ Object
48
49
50
51
|
# File 'lib/objective/filters/array_filter.rb', line 48
def coerce(raw)
return Array.wrap(raw) if options.wrap
raw
end
|
#coerce_error(coerced) ⇒ Object
53
54
55
|
# File 'lib/objective/filters/array_filter.rb', line 53
def coerce_error(coerced)
return :array unless coerced.is_a?(Array)
end
|
#feed(raw) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/objective/filters/array_filter.rb', line 14
def feed(raw)
result = super(raw)
return result if result == Objective::DISCARD || result.errors || result.inputs.nil?
inputs = []
errors = Objective::Errors::ErrorArray.new
sub_filter = sub_filters.first
index_shift = 0
data = result.coerced
data.each_with_index do |sub_data, index|
sub_result = sub_filter.feed(sub_data)
if sub_result == Objective::DISCARD
index_shift += 1
next
end
sub_data = sub_result.inputs
sub_error = sub_result.errors
unless sub_error.nil?
errors[index - index_shift] = sub_filter.handle_errors(sub_error)
end
inputs[index - index_shift] = sub_data
end
result.inputs = inputs
result.errors = errors.present? ? errors : nil
result
end
|