Class: ParameterFilter::CompiledFilter
- Inherits:
-
Object
- Object
- ParameterFilter::CompiledFilter
- Defined in:
- lib/grape_logging/util/parameter_filter.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#blocks ⇒ Object
readonly
Returns the value of attribute blocks.
-
#deep_regexps ⇒ Object
readonly
Returns the value of attribute deep_regexps.
-
#regexps ⇒ Object
readonly
Returns the value of attribute regexps.
Class Method Summary collapse
Instance Method Summary collapse
- #call(original_params, parents = []) ⇒ Object
-
#initialize(replacement, regexps, deep_regexps, blocks) ⇒ CompiledFilter
constructor
A new instance of CompiledFilter.
Constructor Details
#initialize(replacement, regexps, deep_regexps, blocks) ⇒ CompiledFilter
Returns a new instance of CompiledFilter.
66 67 68 69 70 71 |
# File 'lib/grape_logging/util/parameter_filter.rb', line 66 def initialize(replacement, regexps, deep_regexps, blocks) @replacement = replacement @regexps = regexps @deep_regexps = deep_regexps.any? ? deep_regexps : nil @blocks = blocks end |
Instance Attribute Details
#blocks ⇒ Object (readonly)
Returns the value of attribute blocks.
64 65 66 |
# File 'lib/grape_logging/util/parameter_filter.rb', line 64 def blocks @blocks end |
#deep_regexps ⇒ Object (readonly)
Returns the value of attribute deep_regexps.
64 65 66 |
# File 'lib/grape_logging/util/parameter_filter.rb', line 64 def deep_regexps @deep_regexps end |
#regexps ⇒ Object (readonly)
Returns the value of attribute regexps.
64 65 66 |
# File 'lib/grape_logging/util/parameter_filter.rb', line 64 def regexps @regexps end |
Class Method Details
.compile(replacement, filters) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/grape_logging/util/parameter_filter.rb', line 39 def self.compile(replacement, 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 replacement, regexps, deep_regexps, blocks end |
Instance Method Details
#call(original_params, parents = []) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/grape_logging/util/parameter_filter.rb', line 73 def call(original_params, parents = []) filtered_params = {} original_params.each do |key, value| parents.push(key) if deep_regexps if regexps.any? { |r| key =~ r } value = @replacement elsif deep_regexps && (joined = parents.join('.')) && deep_regexps.any? { |r| joined =~ r } value = @replacement 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 |