class Eaternet::ValidatedObject::TypeValidator

A custom validator which ensures an object is a certain class. It's here as a nested class in {ValidatedObject} for easy access by subclasses.

@example Ensure that weight is a floating point number

class Dog < ValidatedObject
  attr_accessor :weight
  validates :weight, type: Float
end

Public Instance Methods

validate_each(record, attribute, value) click to toggle source

@return [nil]

# File lib/eaternet/validated_object.rb, line 72
def validate_each(record, attribute, value)
  expected = options[:with]
  actual = value.class
  return if actual == expected

  msg = options[:message] || "is class #{actual}, not #{expected}"
  record.errors.add attribute, msg
end