Class: Hermod::Validators::TypeChecker

Inherits:
Base
  • Object
show all
Defined in:
lib/hermod/validators/type_checker.rb

Overview

This checks if the given value is an instance of a certain class

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#valid?

Constructor Details

#initialize(expected_class, &block) ⇒ TypeChecker

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 }


19
20
21
22
# 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

Instance Attribute Details

#checkerObject (readonly)

Returns the value of attribute checker.



8
9
10
# File 'lib/hermod/validators/type_checker.rb', line 8

def checker
  @checker
end

#expected_classObject (readonly)

Returns the value of attribute expected_class.



8
9
10
# File 'lib/hermod/validators/type_checker.rb', line 8

def expected_class
  @expected_class
end