contracts 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/builtin_contracts.rb +3 -1
- data/lib/contracts.rb +4 -0
- data/lib/decorators.rb +6 -2
- data/lib/foo.rb +8 -4
- metadata +3 -3
data/lib/builtin_contracts.rb
CHANGED
@@ -10,7 +10,7 @@ And then use these or write your own!
|
|
10
10
|
|
11
11
|
A simple example:
|
12
12
|
|
13
|
-
Contract Num, Num
|
13
|
+
Contract Num, Num => Num
|
14
14
|
def add(a, b)
|
15
15
|
a + b
|
16
16
|
end
|
@@ -353,6 +353,8 @@ module Contracts
|
|
353
353
|
end
|
354
354
|
end
|
355
355
|
|
356
|
+
# Used to define contracts on functions passed in as arguments.
|
357
|
+
# Example: <tt>Func[Num => Num] # the function should take a number and return a number</tt>
|
356
358
|
class Func < CallableClass
|
357
359
|
attr_reader :contracts
|
358
360
|
def initialize(*contracts)
|
data/lib/contracts.rb
CHANGED
data/lib/decorators.rb
CHANGED
@@ -53,11 +53,15 @@ module MethodDecorators
|
|
53
53
|
class_eval <<-ruby_eval, __FILE__, __LINE__ + 1
|
54
54
|
def #{is_class_method ? "self." : ""}#{name}(*args, &blk)
|
55
55
|
ret = nil
|
56
|
-
self
|
56
|
+
this = self#{is_class_method ? "" : ".class"}
|
57
|
+
unless this.respond_to?(:decorated_methods) && !this.decorated_methods.nil?
|
58
|
+
raise "Couldn't find decorator for method " + self.class.name + ":#{name}.\nDoes this method look correct to you? If you are using contracts from rspec, rspec wraps classes in it's own class.\nLook at the specs for contracts.ruby as an example of how to write contracts in this case."
|
59
|
+
end
|
60
|
+
this.decorated_methods[#{name.inspect}].each do |decorator|
|
57
61
|
ret = decorator.call_with(self, *args, &blk)
|
58
62
|
end
|
59
63
|
ret
|
60
|
-
end
|
64
|
+
end
|
61
65
|
ruby_eval
|
62
66
|
end
|
63
67
|
|
data/lib/foo.rb
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
require 'contracts'
|
2
2
|
include Contracts
|
3
3
|
|
4
|
-
Contract Num => Num
|
5
|
-
def
|
6
|
-
|
4
|
+
Contract ArrayOf[Num], Func[Num => Num] => ArrayOf[Num]
|
5
|
+
def map(arr, func)
|
6
|
+
ret = []
|
7
|
+
arr.each do |x|
|
8
|
+
ret << func[x]
|
9
|
+
end
|
10
|
+
ret
|
7
11
|
end
|
8
12
|
|
9
|
-
p
|
13
|
+
p map([1, 2, 3], lambda { |x| x + 1 })
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contracts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Aditya Bhargava
|