handshake 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/handshake/block_contract.rb +24 -29
- data/lib/handshake/version.rb +1 -1
- data/lib/handshake.rb +2 -2
- metadata +2 -2
@@ -1,36 +1,31 @@
|
|
1
|
-
|
2
|
-
|
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
|
-
|
12
|
-
|
13
|
-
|
5
|
+
def initialize(contract, &block)
|
6
|
+
@contract = contract
|
7
|
+
super(&block)
|
8
|
+
end
|
14
9
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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.
|
data/lib/handshake/version.rb
CHANGED
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
|
-
|
554
|
-
return_val = @proxied.send(meth_name, *args) { |*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.
|
7
|
-
date: 2007-05-
|
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
|