Class: Sidekiq::Logging::ArgumentFilter::CompiledFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/logging/argument_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.



54
55
56
57
58
# File 'lib/sidekiq/logging/argument_filter.rb', line 54

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.



52
53
54
# File 'lib/sidekiq/logging/argument_filter.rb', line 52

def blocks
  @blocks
end

#deep_regexpsObject (readonly)

Returns the value of attribute deep_regexps.



52
53
54
# File 'lib/sidekiq/logging/argument_filter.rb', line 52

def deep_regexps
  @deep_regexps
end

#regexpsObject (readonly)

Returns the value of attribute regexps.



52
53
54
# File 'lib/sidekiq/logging/argument_filter.rb', line 52

def regexps
  @regexps
end

Class Method Details

.compile(filters) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sidekiq/logging/argument_filter.rb', line 29

def self.compile(filters)
  return ->(args) { args.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?('\\.') }
  deep_strings, strings = strings.partition { |s| s.include?('\\.') }
  regexps << Regexp.new(strings.join('|'), true) unless strings.empty?
  deep_regexps << Regexp.new(deep_strings.join('|'), true) unless deep_strings.empty?
  new regexps, deep_regexps, blocks
end

Instance Method Details

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sidekiq/logging/argument_filter.rb', line 60

def call(original_args, parents = [])
  filtered_args = {}
  original_args.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 = begin
              key.dup
            rescue StandardError
              key
            end
      value = begin
                value.dup
              rescue StandardError
                value
              end
      blocks.each { |b| b.call(key, value) }
    end
    parents.pop if deep_regexps
    filtered_args[key] = value
  end
  filtered_args
end