class Mutations::ArrayFilter

Public Class Methods

new(name, opts = {}, &block) click to toggle source
Calls superclass method
# File lib/mutations/array_filter.rb, line 17
def initialize(name, opts = {}, &block)
  super(opts)

  @name = name
  @element_filter = nil

  if block_given?
    instance_eval(&block)
  end

  raise ArgumentError.new("Can't supply both a class and a filter") if @element_filter && self.options[:class]
end
register_additional_filter(type_class, type_name) click to toggle source
# File lib/mutations/array_filter.rb, line 3
def self.register_additional_filter(type_class, type_name)
  define_method(type_name) do |options = {}, &block|
    @element_filter = type_class.new(options, &block)
  end
end

Public Instance Methods

array(options = {}, &block) click to toggle source
# File lib/mutations/array_filter.rb, line 38
def array(options = {}, &block)
  @element_filter = ArrayFilter.new(nil, options, &block)
end
filter(data) click to toggle source
# File lib/mutations/array_filter.rb, line 56
def filter(data)
  initialize_constants!

  # Handle nil case
  if data.nil?
    return [nil, nil] if options[:nils]
    return [nil, :nils]
  end

  if !data.is_a?(Array) && options[:arrayize]
    return [[], nil] if data == ""
    data = Array(data)
  end

  if data.is_a?(Array)
    errors = ErrorArray.new
    filtered_data = []
    found_error = false

    return [data, :min_length] if options[:min_length] && data.length < options[:min_length]
    return [data, :max_length] if options[:max_length] && data.length > options[:max_length]
    data.each_with_index do |el, i|
      el_filtered, el_error = filter_element(el)
      el_error = ErrorAtom.new(@name, el_error, :index => i) if el_error.is_a?(Symbol)
      errors << el_error
      if el_error
        found_error = true
      else
        filtered_data << el_filtered
      end
    end

    if found_error && !(@element_filter && @element_filter.discard_invalid?)
      [data, errors]
    else
      [filtered_data, nil]
    end
  else
    return [data, :array]
  end
end
filter_element(data) click to toggle source

Returns [filtered, errors]

# File lib/mutations/array_filter.rb, line 99
def filter_element(data)
  if @element_filter
    data, el_errors = @element_filter.filter(data)
    return [data, el_errors] if el_errors
  elsif options[:class]
    if !data.is_a?(options[:class])
      return [data, :class]
    end
  end

  [data, nil]
end
hash(options = {}, &block) click to toggle source
# File lib/mutations/array_filter.rb, line 30
def hash(options = {}, &block)
  @element_filter = HashFilter.new(options, &block)
end
initialize_constants!() click to toggle source
# File lib/mutations/array_filter.rb, line 42
def initialize_constants!
  @initialize_constants ||= begin
    if options[:class]
      options[:class] = options[:class].constantize if options[:class].is_a?(String)
    end

    true
  end

  unless Mutations.cache_constants?
    options[:class] = options[:class].to_s.constantize if options[:class]
  end
end
model(name, options = {}) click to toggle source
# File lib/mutations/array_filter.rb, line 34
def model(name, options = {})
  @element_filter = ModelFilter.new(name.to_sym, options)
end