class ActiveModel::Validations::FormatValidator

Public Instance Methods

check_validity!() click to toggle source
# File lib/active_model/validations/format.rb, line 15
def check_validity!
  unless options.include?(:with) ^ options.include?(:without)  # ^ == xor, or "exclusive or"
    raise ArgumentError, "Either :with or :without must be supplied (but not both)"
  end

  check_options_validity(options, :with)
  check_options_validity(options, :without)
end
validate_each(record, attribute, value) click to toggle source
# File lib/active_model/validations/format.rb, line 5
def validate_each(record, attribute, value)
  if options[:with]
    regexp = option_call(record, :with)
    record_error(record, attribute, :with, value) if value.to_s !~ regexp
  elsif options[:without]
    regexp = option_call(record, :without)
    record_error(record, attribute, :without, value) if value.to_s =~ regexp
  end
end

Private Instance Methods

check_options_validity(options, name) click to toggle source
# File lib/active_model/validations/format.rb, line 35
def check_options_validity(options, name)
  option = options[name]
  if option && !option.is_a?(Regexp) && !option.respond_to?(:call)
    raise ArgumentError, "A regular expression or a proc or lambda must be supplied as :#{name}"
  end
end
option_call(record, name) click to toggle source
# File lib/active_model/validations/format.rb, line 26
def option_call(record, name)
  option = options[name]
  option.respond_to?(:call) ? option.call(record) : option
end
record_error(record, attribute, name, value) click to toggle source
# File lib/active_model/validations/format.rb, line 31
def record_error(record, attribute, name, value)
  record.errors.add(attribute, :invalid, options.except(name).merge!(:value => value))
end