class Hermod::Validators::TypeChecker
This checks if the given value is an instance of a certain class
def initialize(expected_class, &block)
TypeChecker.new(Date) { |value| value.respond_to? :strftime }
TypeChecker.new(Integer)
Examples
optionally pass a block to customise the class matching logic
Sets up the validator with the class it is expected to be. You can
def initialize(expected_class, &block) @expected_class = expected_class @checker = block || proc { |value| value.is_a? expected_class } end
def message(value, attributes)
def message(value, attributes) expected_class_name = expected_class.name.downcase join_word = (%w(a e i o u).include?(expected_class_name[0]) ? "an" : "a") "must be #{join_word} #{expected_class_name}" end
def test(value, attributes)
def test(value, attributes) value.blank? || checker.call(value) end