class Hermod::Validators::TypeChecker
This checks if the given value is an instance of a certain class
Attributes
checker[R]
expected_class[R]
Public Class Methods
new(expected_class, &block)
click to toggle source
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 }
# File lib/hermod/validators/type_checker.rb, line 19 def initialize(expected_class, &block) @expected_class = expected_class @checker = block || proc { |value| value.is_a? expected_class } end
Private Instance Methods
message(value, attributes)
click to toggle source
# File lib/hermod/validators/type_checker.rb, line 30 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
test(value, attributes)
click to toggle source
# File lib/hermod/validators/type_checker.rb, line 26 def test(value, attributes) value.blank? || checker.call(value) end