5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/literal/modifiers/sig.rb', line 5
def sig(method_name, **signature)
raise ArgumentError unless signature.size == 1
types = signature.keys[0]
return_type = signature.values[0]
keywords = types[-1].is_a?(Hash) ? types.pop : {}
original_method = instance_method(method_name)
define_method(method_name) do |*a, **k, &b|
types.each_with_index do |type, index|
unless type === a[index]
raise Literal::TypeError.expected(a[index], to_be_a: type)
end
end
keywords.each do |key, type|
unless type === k[key]
raise Literal::TypeError.expected(k[key], to_be_a: type)
end
end
output = original_method.bind(self).call(*a, **k, &b)
unless return_type === output
raise Literal::TypeError.expected(output, to_be_a: return_type)
end
output
end
method_name
end
|