Class: ActionDispatch::Http::ParameterFilter::CompiledFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/action_dispatch/http/parameter_filter.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexps, deep_regexps, blocks) ⇒ CompiledFilter

Returns a new instance of CompiledFilter.



50
51
52
53
54
# File 'lib/action_dispatch/http/parameter_filter.rb', line 50

def initialize(regexps, deep_regexps, blocks)
  @regexps = regexps
  @deep_regexps = deep_regexps.any? ? deep_regexps : nil
  @blocks = blocks
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



48
49
50
# File 'lib/action_dispatch/http/parameter_filter.rb', line 48

def blocks
  @blocks
end

#deep_regexpsObject (readonly)

Returns the value of attribute deep_regexps.



48
49
50
# File 'lib/action_dispatch/http/parameter_filter.rb', line 48

def deep_regexps
  @deep_regexps
end

#regexpsObject (readonly)

Returns the value of attribute regexps.



48
49
50
# File 'lib/action_dispatch/http/parameter_filter.rb', line 48

def regexps
  @regexps
end

Class Method Details

.compile(filters) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/action_dispatch/http/parameter_filter.rb', line 23

def self.compile(filters)
  return lambda { |params| params.dup } if filters.empty?

  strings, regexps, blocks = [], [], []

  filters.each do |item|
    case item
    when Proc
      blocks << item
    when Regexp
      regexps << item
    else
      strings << Regexp.escape(item.to_s)
    end
  end

  deep_regexps, regexps = regexps.partition { |r| r.to_s.include?("\\.".freeze) }
  deep_strings, strings = strings.partition { |s| s.include?("\\.".freeze) }

  regexps << Regexp.new(strings.join("|".freeze), true) unless strings.empty?
  deep_regexps << Regexp.new(deep_strings.join("|".freeze), true) unless deep_strings.empty?

  new regexps, deep_regexps, blocks
end

Instance Method Details

#call(original_params, parents = []) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/action_dispatch/http/parameter_filter.rb', line 56

def call(original_params, parents = [])
  filtered_params = original_params.class.new

  original_params.each do |key, value|
    parents.push(key) if deep_regexps
    if regexps.any? { |r| key =~ r }
      value = FILTERED
    elsif deep_regexps && (joined = parents.join(".")) && deep_regexps.any? { |r| joined =~ r }
      value = FILTERED
    elsif value.is_a?(Hash)
      value = call(value, parents)
    elsif value.is_a?(Array)
      value = value.map { |v| v.is_a?(Hash) ? call(v, parents) : v }
    elsif blocks.any?
      key = key.dup if key.duplicable?
      value = value.dup if value.duplicable?
      blocks.each { |b| b.call(key, value) }
    end
    parents.pop if deep_regexps

    filtered_params[key] = value
  end

  filtered_params
end