Class: Contracts::AnnotatedMethod
- Inherits:
-
Object
- Object
- Contracts::AnnotatedMethod
- Defined in:
- lib/contracts.rb
Instance Method Summary collapse
-
#initialize(method, annotations) ⇒ AnnotatedMethod
constructor
A new instance of AnnotatedMethod.
- #invoke(receiver, *args, &blk) ⇒ Object
Constructor Details
#initialize(method, annotations) ⇒ AnnotatedMethod
Returns a new instance of AnnotatedMethod.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/contracts.rb', line 44 def initialize(method, annotations) @method = method @before_annotations = annotations.select { |annotation| annotation.respond_to?(:before_call) } @after_annotations = annotations.select { |annotation| annotation.respond_to?(:after_call) } @exception_annotations = annotations.select { |annotation| annotation.respond_to?(:on_exception) } annotations.each do |annotation| annotation.method = method end end |
Instance Method Details
#invoke(receiver, *args, &blk) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/contracts.rb', line 56 def invoke(receiver, *args, &blk) @before_annotations.each do |annotation| annotation.before_call(receiver, *args, &blk) end # instance methods are UnboundMethod, class methods are Method. rv = @method.is_a?(Method) ? @method.call(*args, &blk) : @method.bind(receiver).call(*args, &blk) @after_annotations.each do |annotation| annotation.after_call(rv, receiver, *args, &blk) end return rv rescue StandardError => exc @exception_annotations.each do |annotation| annotation.on_exception(exc, receiver, *args, &blk) end raise exc end |