class SimpleValidate::ValidatesTypeOf

Constants

SUPPORTED_TYPES

Public Class Methods

new(attribute, options) click to toggle source
Calls superclass method SimpleValidate::ValidatesBase::new
# File lib/simple_validate/validates_type_of.rb, line 7
def initialize(attribute, options)
  @allow_nil = options[:allow_nil]
  @type = options[:as]

  raise ArgumentError unless @type && SUPPORTED_TYPES.include?(@type)

  super(attribute, options[:message] ||
    "must be #{Utils.article(@type)} #{@type}", options[:if] || proc { true })
end

Public Instance Methods

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

  return true if val.nil? && @allow_nil == true

  if @type == :boolean
    [true, false].include? val
  else
    klass = Utils.classify(@type)
    val.is_a?(klass)
  end
end