Sha256: 7aeb314f90fe10980cbee9ad89c9f50c42029c524ce750161c050bc01d37985d

Contents?: true

Size: 980 Bytes

Versions: 4

Compression:

Stored size: 980 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
      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

    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

4 entries across 4 versions & 2 rubygems

Version Path
approvals-0.0.26 lib/approvals/filter.rb
approvals-0.0.25 lib/approvals/filter.rb
approvals-0.0.24 lib/approvals/filter.rb
br-approvals-0.0.22 lib/approvals/filter.rb