aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2012-10-18 17:38:03 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2012-10-18 17:38:03 -0700
commit79e91cc0ec2312bd3257ce0ef3323e1add65a36f (patch)
treeee08b3c4df27fe188ea03320029fb8e97fad14a8 /actionpack
parentbe9fc772dc8ec0658014261b31c98df9c518dc35 (diff)
downloadrails-79e91cc0ec2312bd3257ce0ef3323e1add65a36f.tar.gz
rails-79e91cc0ec2312bd3257ce0ef3323e1add65a36f.tar.bz2
rails-79e91cc0ec2312bd3257ce0ef3323e1add65a36f.zip
move compiled filters to their own class so that recursive calls don't
need to check for the length of @filters
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/http/parameter_filter.rb86
1 files changed, 45 insertions, 41 deletions
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