class Rory::ParameterFilter::CompiledFilter

Attributes

blocks[R]
regexps[R]

Public Class Methods

compile(filters) click to toggle source
# File lib/rory/parameter_filter.rb, line 20
def self.compile(filters)
  return lambda { |params| params.dup } if filters.empty?

  strings, regexps, blocks = [], [], []

  filters.each do |item|
    case item
      when Regexp
        regexps << item
      else
        strings << item.to_s
    end
  end

  regexps << Regexp.new(strings.join('|'), true) unless strings.empty?
  new regexps, blocks
end
new(regexps, blocks) click to toggle source
# File lib/rory/parameter_filter.rb, line 40
def initialize(regexps, blocks)
  @regexps = regexps
  @blocks  = blocks
end

Public Instance Methods

call(original_params) click to toggle source
# File lib/rory/parameter_filter.rb, line 45
def call(original_params)
  filtered_params = {}

  original_params.each do |key, value|
    if regexps.any? { |r| key =~ r }
      value = FILTERED
    elsif value.is_a?(Hash)
      value = call(value)
    elsif value.is_a?(Array)
      value = value.map { |v| v.is_a?(Hash) ? call(v) : v }
    end

    filtered_params[key] = value
  end

  filtered_params
end