Sha256: 7fcf936ebac3ca0e12c20d5ccf86cfc1ca8b14a8a469ab1699124021269f5de2

Contents?: true

Size: 966 Bytes

Versions: 2

Compression:

Stored size: 966 Bytes

Contents

module Approvals
  class Filter
    attr_reader :filters

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

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

    def censored value, key=nil
      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
        if value.nil?
          nil
        elsif key && placeholder_for(key)
          "<#{placeholder_for(key)}>"
        else
          value
        end
      end
    end

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
approvals-0.0.22 lib/approvals/filter.rb
approvals-0.0.21 lib/approvals/filter.rb