class Mutations::IntegerFilter

Public Instance Methods

filter(data) click to toggle source
# File lib/mutations/integer_filter.rb, line 11
def filter(data)
  # Handle nil case
  if data.nil?
    return [nil, nil] if options[:nils]
    return [nil, :nils]
  end

  # Now check if it's empty:
  if data == ""
    if options[:empty_is_nil]
      return [nil, (:nils unless options[:nils])]
    else
      return [data, :empty]
    end
  end

  # Ensure it's the correct data type (Integer)
  if !data.is_a?(Integer)
    if data.is_a?(String) && data =~ /^-?\d/
      data = data.to_i
    else
      return [data, :integer]
    end
  end

  return [data, :min] if options[:min] && data < options[:min]
  return [data, :max] if options[:max] && data > options[:max]

  # Ensure it matches `in`
  return [data, :in] if options[:in] && !options[:in].include?(data)

  # We win, it's valid!
  [data, nil]
end