10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/pedant.rb', line 10
def returns(sym, *klasses, &block)
old_method = instance_method(sym)
self.send(:define_method, sym) do |*args|
ret = old_method.bind(self).call(*args)
unless klasses.empty? or klasses.any?{|klass| ret.is_a?(klass) }
raise Pedant::TypeError, "Bad return value! Got #{ret.inspect}, " \
"expected instance of #{klasses.inspect}."
end
unless block.nil? or block.call(ret)
raise Pedant::GuardError, "Did not pass user-defined guard."
end
ret
end
end
|