class SimpleValidate::ValidatesFormatOf

Attributes

regex[RW]

Public Class Methods

new(attribute, options) click to toggle source
Calls superclass method SimpleValidate::ValidatesBase::new
# File lib/simple_validate/validates_format_of.rb, line 7
def initialize(attribute, options)
  @allow_nil = options[:allow_nil]
  @regex = options[:with]
  super(
    attribute,
    options[:message] || "is incorrect format",
    options[:if] || proc { true }
  )
end

Public Instance Methods

valid?(instance) click to toggle source
# File lib/simple_validate/validates_format_of.rb, line 17
def valid?(instance)
  val = instance.send(attribute)

  return true if val.nil? && @allow_nil == true
  raise ArgumentError if regex.nil? || !regex.is_a?(Regexp)

  !!(val =~ regex)
end