handshake 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,36 +1,31 @@
1
- class Proc
2
- attr_reader :contract
3
-
4
- # Define a contract on this Proc. When the contract is set, redefine this
5
- # proc's call method to ensure that values that pass through the proc are
6
- # checked according to the contract.
7
- def contract=(proc_contract)
8
- @contract = proc_contract
9
- end
1
+ module Handshake
2
+ class CheckedProc < Proc
3
+ attr_accessor :contract
10
4
 
11
- def checked?
12
- not @contract.nil?
13
- end
5
+ def initialize(contract, &block)
6
+ @contract = contract
7
+ super(&block)
8
+ end
14
9
 
15
- def checked_call(*args)
16
- Handshake.catch_contract("Contract violated in call to proc #{self}") do
17
- @contract.check_accepts! *args
18
- end if checked?
19
-
20
- return_value = orig_call(*args)
21
-
22
- Handshake.catch_contract("Contract violated by proc #{self}") do
23
- @contract.check_returns! return_value
24
- end if checked?
25
-
26
- return_value
10
+ def checked?
11
+ not @contract.nil?
12
+ end
13
+
14
+ def call(*args)
15
+ Handshake.catch_contract("Contract violated in call to proc #{self}") do
16
+ @contract.check_accepts! *args
17
+ end if checked?
18
+
19
+ return_value = super(*args)
20
+
21
+ Handshake.catch_contract("Contract violated by proc #{self}") do
22
+ @contract.check_returns! return_value
23
+ end if checked?
24
+
25
+ return_value
26
+ end
27
27
  end
28
- alias :orig_call :call
29
- alias :call :checked_call
30
28
 
31
- end
32
-
33
- module Handshake
34
29
  # For block-checking, we need a class which is_a? Proc for instance checking
35
30
  # purposes but isn't the same so as not to prevent the user from passing in
36
31
  # explicitly defined procs as arguments.
@@ -2,7 +2,7 @@ module Handshake # :nodoc:
2
2
  module VERSION # :nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 2
5
- TINY = 0
5
+ TINY = 1
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
data/lib/handshake.rb CHANGED
@@ -550,8 +550,8 @@ module Handshake
550
550
  # contract checks work if receiver uses yield.
551
551
  return_val = nil
552
552
  if contract.expects_block?
553
- block.contract = contract.block_contract
554
- return_val = @proxied.send(meth_name, *args) { |*argz| block.call(*argz) }
553
+ cp = CheckedProc.new(contract.block_contract, &block)
554
+ return_val = @proxied.send(meth_name, *args) { |*argz| cp.call(*argz) }
555
555
  else
556
556
  return_val = @proxied.send(meth_name, *args, &block)
557
557
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: handshake
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.0
7
- date: 2007-05-01 00:00:00 -04:00
6
+ version: 0.2.1
7
+ date: 2007-05-02 00:00:00 -04:00
8
8
  summary: Handshake is a simple design-by-contract system for Ruby.
9
9
  require_paths:
10
10
  - lib