Class: Approvals::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/approvals/filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filters) ⇒ Filter

Returns a new instance of Filter


5
6
7
8
# File 'lib/approvals/filter.rb', line 5

def initialize(filters)
  @filters = filters
  @placeholder = {}
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters


3
4
5
# File 'lib/approvals/filter.rb', line 3

def filters
  @filters
end

Instance Method Details

#apply(hash_or_array) ⇒ Object


10
11
12
13
14
15
16
# File 'lib/approvals/filter.rb', line 10

def apply hash_or_array
  if @filters.any?
    censored(hash_or_array)
  else
    hash_or_array
  end
end

#censored(value, key = nil) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/approvals/filter.rb', line 18

def censored value, key=nil
  if value.nil?
    nil
  elsif key && placeholder_for(key)
    "<#{placeholder_for(key)}>"
  else
    case value
      when Array
        value.map { |item| censored(item) }
      when Hash
        Hash[value.map { |inner_key, inner_value| [inner_key, censored(inner_value, inner_key)] }]
      else
        value
    end
  end
end

#placeholder_for(key) ⇒ Object


35
36
37
38
39
40
41
42
43
# File 'lib/approvals/filter.rb', line 35

def placeholder_for key
  return @placeholder[key] if @placeholder.key? key

  applicable_filters = filters.select do |placeholder, pattern|
    pattern && key.match(pattern)
  end

  @placeholder[key] = applicable_filters.keys.last
end