Class: Mutations::ArrayFilter
Instance Attribute Summary
Attributes inherited from InputFilter
#options
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from InputFilter
#default, default_options, #discard_empty?, #discard_invalid?, #discard_nils?, #has_default?
Constructor Details
#initialize(name, opts = {}, &block) ⇒ ArrayFilter
Returns a new instance of ArrayFilter.
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/mutations/array_filter.rb', line 18
def initialize(name, opts = {}, &block)
super(opts)
@name = name
@element_filter = nil
if block_given?
instance_eval(&block)
end
raise ArgumentError.new("Can't supply both a class and a filter") if @element_filter && self.options[:class]
end
|
Class Method Details
.register_additional_filter(type_class, type_name) ⇒ Object
3
4
5
6
7
8
|
# File 'lib/mutations/array_filter.rb', line 3
def self.register_additional_filter(type_class, type_name)
define_method(type_name) do | *args, &block |
options = args[0] || {}
@element_filter = type_class.new(options, &block)
end
end
|
Instance Method Details
#array(options = {}, &block) ⇒ Object
39
40
41
|
# File 'lib/mutations/array_filter.rb', line 39
def array(options = {}, &block)
@element_filter = ArrayFilter.new(nil, options, &block)
end
|
#filter(data) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/mutations/array_filter.rb', line 57
def filter(data)
initialize_constants!
if data.nil?
return [nil, nil] if options[:nils]
return [nil, :nils]
end
if !data.is_a?(Array) && options[:arrayize]
return [[], nil] if data == ""
data = Array(data)
end
if data.is_a?(Array)
errors = ErrorArray.new
filtered_data = []
found_error = false
return [data, :min_length] if options[:min_length] && data.length < options[:min_length]
return [data, :max_length] if options[:max_length] && data.length > options[:max_length]
data.each_with_index do |el, i|
el_filtered, el_error = filter_element(el)
el_error = ErrorAtom.new(@name, el_error, :index => i) if el_error.is_a?(Symbol)
errors << el_error
if el_error
found_error = true
else
filtered_data << el_filtered
end
end
if found_error && !(@element_filter && @element_filter.discard_invalid?)
[data, errors]
else
[filtered_data, nil]
end
else
return [data, :array]
end
end
|
#filter_element(data) ⇒ Object
Returns [filtered, errors]
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/mutations/array_filter.rb', line 100
def filter_element(data)
if @element_filter
data, el_errors = @element_filter.filter(data)
return [data, el_errors] if el_errors
elsif options[:class]
if !data.is_a?(options[:class])
return [data, :class]
end
end
[data, nil]
end
|
#hash(options = {}, &block) ⇒ Object
31
32
33
|
# File 'lib/mutations/array_filter.rb', line 31
def hash(options = {}, &block)
@element_filter = HashFilter.new(options, &block)
end
|
#initialize_constants! ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/mutations/array_filter.rb', line 43
def initialize_constants!
@initialize_constants ||= begin
if options[:class]
options[:class] = options[:class].constantize if options[:class].is_a?(String)
end
true
end
unless Mutations.cache_constants?
options[:class] = options[:class].to_s.constantize if options[:class]
end
end
|
#model(name, options = {}) ⇒ Object
35
36
37
|
# File 'lib/mutations/array_filter.rb', line 35
def model(name, options = {})
@element_filter = ModelFilter.new(name.to_sym, options)
end
|