Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

move compiled filters to their own class so that recursive calls don't

need to check for the length of @filters
  • Loading branch information...
commit 79e91cc0ec2312bd3257ce0ef3323e1add65a36f 1 parent be9fc77
@tenderlove tenderlove authored
Showing with 45 additions and 41 deletions.
  1. +45 −41 actionpack/lib/action_dispatch/http/parameter_filter.rb
View
86 actionpack/lib/action_dispatch/http/parameter_filter.rb
@@ -1,68 +1,72 @@
module ActionDispatch
module Http
class ParameterFilter
+ FILTERED = '[FILTERED]'.freeze # :nodoc:
+
def initialize(filters)
@filters = filters
end
def filter(params)
- if @filters.empty?
- params.dup
- else
- compiled_filter.call(params)
- end
+ compiled_filter.call(params)
end
private
- FILTERED = '[FILTERED]'.freeze
-
def compiled_filter
- @compiled_filter ||= begin
- regexps, blocks = compile_filter
+ @compiled_filter ||= CompiledFilter.compile(@filters)
+ end
- lambda do |original_params|
- filtered_params = {}
+ class CompiledFilter # :nodoc:
+ def self.compile(filters)
+ return lambda { |params| params.dup } if filters.empty?
- original_params.each do |key, value|
- if regexps.find { |r| key =~ r }
- value = FILTERED
- elsif value.is_a?(Hash)
- value = filter(value)
- elsif value.is_a?(Array)
- value = value.map { |v| v.is_a?(Hash) ? filter(v) : v }
- elsif blocks.present?
- key = key.dup
- value = value.dup if value.duplicable?
- blocks.each { |b| b.call(key, value) }
- end
+ strings, regexps, blocks = [], [], []
- filtered_params[key] = value
+ filters.each do |item|
+ case item
+ when Proc
+ blocks << item
+ when Regexp
+ regexps << item
+ else
+ strings << item.to_s
end
-
- filtered_params
end
+
+ regexps << Regexp.new(strings.join('|'), true) unless strings.empty?
+ new regexps, blocks
end
- end
- def compile_filter
- strings, regexps, blocks = [], [], []
+ attr_reader :regexps, :blocks
- @filters.each do |item|
- case item
- when Proc
- blocks << item
- when Regexp
- regexps << item
- else
- strings << item.to_s
- end
+ def initialize(regexps, blocks)
+ @regexps = regexps
+ @blocks = blocks
end
- regexps << Regexp.new(strings.join('|'), true) unless strings.empty?
- [regexps, blocks]
- end
+ def call(original_params)
+ filtered_params = {}
+
+ original_params.each do |key, value|
+ if regexps.any? { |r| key =~ r }
+ value = FILTERED
+ elsif value.is_a?(Hash)
+ value = call(value)
+ elsif value.is_a?(Array)
+ value = value.map { |v| v.is_a?(Hash) ? call(v) : v }
+ elsif blocks.any?
+ key = key.dup
+ value = value.dup if value.duplicable?
+ blocks.each { |b| b.call(key, value) }
+ end
+ filtered_params[key] = value
+ end
+
+ filtered_params
+ end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.