@@ -40,97 +40,83 @@ class ParameterFilter
40
40
#
41
41
# * <tt>:mask</tt> - A replaced object when filtered. Defaults to <tt>"[FILTERED]"</tt>.
42
42
def initialize ( filters = [ ] , mask : FILTERED )
43
- @filters = filters
44
43
@mask = mask
44
+ compile_filters! ( filters )
45
45
end
46
46
47
47
# Mask value of +params+ if key matches one of filters.
48
48
def filter ( params )
49
- compiled_filter . call ( params )
49
+ @no_filters ? params . dup : call ( params )
50
50
end
51
51
52
52
# Returns filtered value for given key. For +Proc+ filters, third block argument is not populated.
53
53
def filter_param ( key , value )
54
- @filters . empty? ? value : compiled_filter . value_for_key ( key , value )
54
+ @no_filters ? value : value_for_key ( key , value )
55
55
end
56
56
57
57
private
58
- def compiled_filter
59
- @compiled_filter ||= CompiledFilter . compile ( @filters , mask : @mask )
60
- end
61
-
62
- class CompiledFilter # :nodoc:
63
- def self . compile ( filters , mask :)
64
- return lambda { |params | params . dup } if filters . empty?
65
-
66
- strings , regexps , blocks , deep_regexps , deep_strings = [ ] , [ ] , [ ] , nil , nil
67
-
68
- filters . each do |item |
69
- case item
70
- when Proc
71
- blocks << item
72
- when Regexp
73
- if item . to_s . include? ( "\\ ." )
74
- ( deep_regexps ||= [ ] ) << item
75
- else
76
- regexps << item
77
- end
58
+ def compile_filters! ( filters )
59
+ @no_filters = filters . empty?
60
+ return if @no_filters
61
+
62
+ @regexps , strings = [ ] , [ ]
63
+ @deep_regexps , deep_strings = nil , nil
64
+ @blocks = nil
65
+
66
+ filters . each do |item |
67
+ case item
68
+ when Proc
69
+ ( @blocks ||= [ ] ) << item
70
+ when Regexp
71
+ if item . to_s . include? ( "\\ ." )
72
+ ( @deep_regexps ||= [ ] ) << item
78
73
else
79
- s = Regexp . escape ( item . to_s )
80
- if s . include? ( "\\ ." )
81
- ( deep_strings ||= [ ] ) << s
82
- else
83
- strings << s
84
- end
74
+ @regexps << item
75
+ end
76
+ else
77
+ s = Regexp . escape ( item . to_s )
78
+ if s . include? ( "\\ ." )
79
+ ( deep_strings ||= [ ] ) << s
80
+ else
81
+ strings << s
85
82
end
86
83
end
87
-
88
- regexps << Regexp . new ( strings . join ( "|" ) , true ) unless strings . empty?
89
- ( deep_regexps ||= [ ] ) << Regexp . new ( deep_strings . join ( "|" ) , true ) if deep_strings &.any?
90
-
91
- new regexps , deep_regexps , blocks , mask : mask
92
- end
93
-
94
- attr_reader :regexps , :deep_regexps , :blocks
95
-
96
- def initialize ( regexps , deep_regexps , blocks , mask :)
97
- @regexps = regexps
98
- @deep_regexps = deep_regexps &.any? ? deep_regexps : nil
99
- @blocks = blocks
100
- @mask = mask
101
84
end
102
85
103
- def call ( params , full_parent_key = nil , original_params = params )
104
- filtered_params = params . class . new
86
+ @regexps << Regexp . new ( strings . join ( "|" ) , true ) unless strings . empty?
87
+ ( @deep_regexps ||= [ ] ) << Regexp . new ( deep_strings . join ( "|" ) , true ) if deep_strings
88
+ end
105
89
106
- params . each do |key , value |
107
- filtered_params [ key ] = value_for_key ( key , value , full_parent_key , original_params )
108
- end
90
+ def call ( params , full_parent_key = nil , original_params = params )
91
+ filtered_params = params . class . new
109
92
110
- filtered_params
93
+ params . each do |key , value |
94
+ filtered_params [ key ] = value_for_key ( key , value , full_parent_key , original_params )
111
95
end
112
96
113
- def value_for_key ( key , value , full_parent_key = nil , original_params = nil )
114
- if deep_regexps
115
- full_key = full_parent_key ? "#{ full_parent_key } .#{ key } " : key . to_s
116
- end
97
+ filtered_params
98
+ end
117
99
118
- if regexps . any? { |r | r . match? ( key . to_s ) }
119
- value = @mask
120
- elsif deep_regexps &.any? { |r | r . match? ( full_key ) }
121
- value = @mask
122
- elsif value . is_a? ( Hash )
123
- value = call ( value , full_key , original_params )
124
- elsif value . is_a? ( Array )
125
- value = value . map { |v | value_for_key ( key , v , full_parent_key , original_params ) }
126
- elsif blocks . any?
127
- key = key . dup if key . duplicable?
128
- value = value . dup if value . duplicable?
129
- blocks . each { |b | b . arity == 2 ? b . call ( key , value ) : b . call ( key , value , original_params ) }
130
- end
100
+ def value_for_key ( key , value , full_parent_key = nil , original_params = nil )
101
+ if @deep_regexps
102
+ full_key = full_parent_key ? "#{ full_parent_key } .#{ key } " : key . to_s
103
+ end
131
104
132
- value
105
+ if @regexps . any? { |r | r . match? ( key . to_s ) }
106
+ value = @mask
107
+ elsif @deep_regexps &.any? { |r | r . match? ( full_key ) }
108
+ value = @mask
109
+ elsif value . is_a? ( Hash )
110
+ value = call ( value , full_key , original_params )
111
+ elsif value . is_a? ( Array )
112
+ value = value . map { |v | value_for_key ( key , v , full_parent_key , original_params ) }
113
+ elsif @blocks
114
+ key = key . dup if key . duplicable?
115
+ value = value . dup if value . duplicable?
116
+ @blocks . each { |b | b . arity == 2 ? b . call ( key , value ) : b . call ( key , value , original_params ) }
133
117
end
118
+
119
+ value
134
120
end
135
121
end
136
122
end
0 commit comments