Module: Locomotive::TypeChecking::TypeChecker

Extended by:
TypeChecker
Included in:
TypeChecker
Defined in:
lib/locomotive/misc/type_check.rb

Overview

Type checking method parameters

Instance Method Summary collapse

Instance Method Details

#check_arg_array_type(elem_type, arg, mtd, n = 0) ⇒ Object

check an array type



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/locomotive/misc/type_check.rb', line 36

def check_arg_array_type(elem_type, arg, mtd, n=0)
  check_arg_type Array, arg, mtd, n
  arg.each_with_index do |x,i|
    unless x.kind_of? elem_type
      raise ArgumentError,
            "#{x.class} assigned to #{elem_type} for " \
            "the #{nth n} element of the #{nth n} "    \
            "argument of #{mtd}."
    end
  end
end

#check_arg_hash_type(key_type, elem_type, arg, mtd, n = 0) ⇒ Object

check a hash type



49
50
51
52
53
54
55
56
# File 'lib/locomotive/misc/type_check.rb', line 49

def check_arg_hash_type(key_type, elem_type, arg, mtd, n=0)
  check_arg_type Hash, arg, mtd, n
  arg.each_with_index do |item,i|
    check_arg_type key_type, item.first, mtd, n
    check_arg_type elem_type, item.last, mtd, n if elem_type.kind_of? Class
    check_arg_array_type elem_type.first, item.last, mtd, n if elem_type.kind_of? Array
  end
end

#check_arg_type(expected, obj, mtd, n = 0) ⇒ Object

check a simple argument type



27
28
29
30
31
32
33
# File 'lib/locomotive/misc/type_check.rb', line 27

def check_arg_type(expected, obj, mtd, n=0)
  unless obj.kind_of? expected
    raise ArgumentError,
          "#{obj.class} assigned to #{expected} " \
          "for the #{nth n} argument of #{mtd}."
  end
end

#check_vararg_type(expected, args, mtd, n = 0) ⇒ Object

check a variable argument type



59
60
61
62
63
# File 'lib/locomotive/misc/type_check.rb', line 59

def check_vararg_type(expected, args, mtd, n=0)
  (n..args.length).each do |i|
    check_arg_type expected, args[i], mtd, i
  end
end