contracts 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/builtin_contracts.rb +9 -0
- data/lib/contracts.rb +15 -1
- metadata +4 -19
- data/lib/foo.rb +0 -9
- data/lib/test.rb +0 -60
data/lib/builtin_contracts.rb
CHANGED
@@ -312,6 +312,15 @@ module Contracts
|
|
312
312
|
end
|
313
313
|
end
|
314
314
|
|
315
|
+
# Takes a Contract.
|
316
|
+
# The contract passes if the contract passes or the given value is nil.
|
317
|
+
# Maybe(foo) is equivalent to Or[foo, nil].
|
318
|
+
class Maybe < Or
|
319
|
+
def initialize(*vals)
|
320
|
+
super(*(vals + [nil]))
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
315
324
|
class ::Hash
|
316
325
|
def testable?
|
317
326
|
self.values.all? do |val|
|
data/lib/contracts.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'decorators'
|
2
2
|
require 'builtin_contracts'
|
3
|
+
|
3
4
|
module Contracts
|
4
5
|
def self.included(base)
|
5
6
|
common base
|
@@ -15,6 +16,15 @@ module Contracts
|
|
15
16
|
def Contract(*args)
|
16
17
|
self.class.Contract(*args)
|
17
18
|
end
|
19
|
+
|
20
|
+
def functype(funcname)
|
21
|
+
contracts = self.class.decorated_methods[funcname]
|
22
|
+
if contracts.nil? || contracts.empty?
|
23
|
+
"No contract for #{self.class}.#{funcname}"
|
24
|
+
else
|
25
|
+
"#{funcname} :: #{contracts[0]}"
|
26
|
+
end
|
27
|
+
end
|
18
28
|
end
|
19
29
|
end
|
20
30
|
end
|
@@ -22,7 +32,7 @@ end
|
|
22
32
|
# This is the main Contract class. When you write a new contract, you'll
|
23
33
|
# write it as:
|
24
34
|
#
|
25
|
-
# Contract [contract names]
|
35
|
+
# Contract [contract names] => return_value
|
26
36
|
#
|
27
37
|
# This class also provides useful callbacks and a validation method.
|
28
38
|
class Contract < Decorator
|
@@ -38,6 +48,10 @@ class Contract < Decorator
|
|
38
48
|
@klass, @method, @contracts = klass, method, contracts
|
39
49
|
end
|
40
50
|
|
51
|
+
def to_s
|
52
|
+
(contracts[0, contracts.size - 1].join(", ") + " => #{contracts[-1]}").gsub("Contracts::", "")
|
53
|
+
end
|
54
|
+
|
41
55
|
# Given a hash, prints out a failure message.
|
42
56
|
# This function is used by the default #failure_callback method
|
43
57
|
# and uses the hash passed into the failure_callback method.
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contracts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 2
|
10
|
-
version: 0.1.2
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.3
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Aditya Bhargava
|
@@ -15,8 +10,7 @@ autorequire:
|
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
12
|
|
18
|
-
date: 2012-06-07 00:00:00
|
19
|
-
default_executable:
|
13
|
+
date: 2012-06-07 00:00:00 Z
|
20
14
|
dependencies: []
|
21
15
|
|
22
16
|
description: This library provides contracts for Ruby. Contracts let you clearly express how your code behaves, and free you from writing tons of boilerplate, defensive code.
|
@@ -31,10 +25,7 @@ files:
|
|
31
25
|
- lib/builtin_contracts.rb
|
32
26
|
- lib/contracts.rb
|
33
27
|
- lib/decorators.rb
|
34
|
-
- lib/foo.rb
|
35
|
-
- lib/test.rb
|
36
28
|
- lib/testable.rb
|
37
|
-
has_rdoc: true
|
38
29
|
homepage: http://github.com/egonSchiele/contracts.ruby
|
39
30
|
licenses: []
|
40
31
|
|
@@ -48,23 +39,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
48
39
|
requirements:
|
49
40
|
- - ">="
|
50
41
|
- !ruby/object:Gem::Version
|
51
|
-
hash: 3
|
52
|
-
segments:
|
53
|
-
- 0
|
54
42
|
version: "0"
|
55
43
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
44
|
none: false
|
57
45
|
requirements:
|
58
46
|
- - ">="
|
59
47
|
- !ruby/object:Gem::Version
|
60
|
-
hash: 3
|
61
|
-
segments:
|
62
|
-
- 0
|
63
48
|
version: "0"
|
64
49
|
requirements: []
|
65
50
|
|
66
51
|
rubyforge_project:
|
67
|
-
rubygems_version: 1.
|
52
|
+
rubygems_version: 1.8.11
|
68
53
|
signing_key:
|
69
54
|
specification_version: 3
|
70
55
|
summary: Contracts for Ruby.
|
data/lib/foo.rb
DELETED
data/lib/test.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'contracts'
|
2
|
-
require 'testable'
|
3
|
-
include Contracts
|
4
|
-
|
5
|
-
class Object
|
6
|
-
|
7
|
-
Contract Num, Num
|
8
|
-
def double(x)
|
9
|
-
x * 2
|
10
|
-
end
|
11
|
-
|
12
|
-
# bug: the `b` here doesn't get typechecked and throws an error.
|
13
|
-
Contract Num, Num, Num
|
14
|
-
def add(a, b="hello!")
|
15
|
-
a + b
|
16
|
-
end
|
17
|
-
|
18
|
-
Contract Proc, nil
|
19
|
-
def run(&blk)
|
20
|
-
puts "running:"
|
21
|
-
blk.call
|
22
|
-
end
|
23
|
-
|
24
|
-
Contract Method, Num
|
25
|
-
def call(func)
|
26
|
-
func.call
|
27
|
-
end
|
28
|
-
|
29
|
-
Contract Args[Num], Num
|
30
|
-
def sum(*vals)
|
31
|
-
vals.inject(0) do |acc, v|
|
32
|
-
acc + v
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
Contract ({ :age => Num, :name => String }), nil
|
37
|
-
def person(data)
|
38
|
-
p data
|
39
|
-
end
|
40
|
-
|
41
|
-
Contract Num, Num
|
42
|
-
def test(x)
|
43
|
-
x + 2
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
# this doesn't work
|
48
|
-
# p Object.send(:sum, 1, 2)
|
49
|
-
|
50
|
-
# but this does:
|
51
|
-
# p send(:sum, 1, 2)
|
52
|
-
#
|
53
|
-
# why???
|
54
|
-
|
55
|
-
Testable.check_all
|
56
|
-
# Testable.check(method(:double))
|
57
|
-
|
58
|
-
# p Object.test(5)
|
59
|
-
#
|
60
|
-
Object.Hello
|