Sha256: 53521db3809a68122422d1a838de5e17a9ddb7548a08107700e209ed8fa79328
Contents?: true
Size: 1.02 KB
Versions: 10
Compression:
Stored size: 1.02 KB
Contents
require 'hermod/validators/base' module Hermod module Validators # This checks if the given value is an instance of a certain class class TypeChecker < Base attr_reader :expected_class, :checker # Sets up the validator with the class it is expected to be. You can # optionally pass a block to customise the class matching logic # # Examples # # TypeChecker.new(Integer) # # TypeChecker.new(Date) { |value| value.respond_to? :strftime } # def initialize(expected_class, &block) @expected_class = expected_class @checker = block || proc { |value| value.is_a? expected_class } end private def test(value, attributes) value.blank? || checker.call(value) end 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 end end end
Version data entries
10 entries across 10 versions & 1 rubygems