class BlueprintAgreement::ExcludeFilter

Public Class Methods

deep_exclude(content, exclude_attributes) click to toggle source
# File lib/blueprint_agreement/utils/exclude_filter.rb, line 10
def deep_exclude(content, exclude_attributes)
  return content if content.blank? || !content.is_a?(Hash)
  new(exclude_attributes).deep_exclude(content)
end
new(exclude_attributes) click to toggle source
# File lib/blueprint_agreement/utils/exclude_filter.rb, line 16
def initialize(exclude_attributes)
  @exclude_attributes = exclude_attributes
end

Public Instance Methods

deep_exclude(content) click to toggle source
# File lib/blueprint_agreement/utils/exclude_filter.rb, line 20
def deep_exclude(content)
  return content if @exclude_attributes.nil?

  content.with_indifferent_access.tap do |params|
    @exclude_attributes.flatten.each do |filter|
      case filter
      when Symbol, String
        params.delete(filter) && params if params.has_key?(filter)
      when Hash then
        hash_filter(params, filter)
      end
    end
  end
end

Private Instance Methods

each_element(object) { |el| ... } click to toggle source
# File lib/blueprint_agreement/utils/exclude_filter.rb, line 52
def each_element(object)
  case object
  when Array
    object.grep(Hash).map { |el| yield el }.compact
  when Hash
    if fields_for_style? object
      hash = object.class.new
      object.each { |k,v| hash[k] = yield v }
      hash
    else
      yield object
    end
  end
end
fields_for_style?(object) click to toggle source
# File lib/blueprint_agreement/utils/exclude_filter.rb, line 67
def fields_for_style?(object)
  object.is_a?(Hash) && object.all? { |k, v| k =~ /\A-?\d+\z/ && v.is_a?(Hash) }
end
hash_filter(params, filter) click to toggle source
# File lib/blueprint_agreement/utils/exclude_filter.rb, line 37
def hash_filter(params, filter)
  filter = filter.with_indifferent_access

  params.slice(*filter.keys).each do |key, value|
    next unless value
    next unless params.has_key? key

    if value.is_a?(Array) || value.is_a?(Hash)
      params[key] = each_element(value) do |element|
        self.class.deep_exclude(element, Array.wrap(filter[key]))
      end
    end
  end
end