Top Level Namespace

Defined Under Namespace

Modules: TypeTempest Classes: TypeMismatchError

Instance Method Summary collapse

Instance Method Details

#check(type, &block) ⇒ Object

Raises:

  • (TypeError)


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/type_tempest.rb', line 8

def check(type, &block)

  type = type.to_s.constantize if defined?(ActiveSupport) && (type.instance_of?(Symbol) || type.instance_of?(String))
  raise TypeError, "`type` must be an object of type Class" unless type.instance_of?(Class)

  var    = yield
  vartyp = eval("#{var.to_s}", block.binding)

  raise TypeMismatchError, "Parameter #{var} should be of type #{type}, but was instead #{vartyp.class}" unless vartyp.is_a?(type)

end

#strict(type, &block) ⇒ Object

Raises:

  • (TypeError)


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/type_tempest.rb', line 21

def strict(type, &block)

  type = type.to_s.constantize if defined?(ActiveSupport) && (type.instance_of?(Symbol) || type.instance_of?(String))
  raise TypeError, "`type` must be an object of type Class" unless type.instance_of?(Class)

  var    = yield
  vartyp = eval("#{var.to_s}", block.binding)

  raise TypeMismatchError, "Parameter #{var} should be of type #{type}, but was instead #{vartyp.class}" unless vartyp.instance_of?(type)

end