From 79e91cc0ec2312bd3257ce0ef3323e1add65a36f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 18 Oct 2012 17:38:03 -0700 Subject: move compiled filters to their own class so that recursive calls don't need to check for the length of @filters --- .../lib/action_dispatch/http/parameter_filter.rb | 86 +++++++++++----------- 1 file changed, 45 insertions(+), 41 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_dispatch/http/parameter_filter.rb b/actionpack/lib/action_dispatch/http/parameter_filter.rb index 583893ccb1..9fe0523e32 100644 --- a/actionpack/lib/action_dispatch/http/parameter_filter.rb +++ b/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 -- cgit v1.2.3