contracts 0.0.8 → 0.0.9

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.
Files changed (3) hide show
  1. data/lib/contracts.rb +8 -15
  2. data/lib/foo.rb +7 -8
  3. metadata +3 -3
data/lib/contracts.rb CHANGED
@@ -20,6 +20,12 @@ class Contract < Decorator
20
20
  attr_accessor :contracts, :klass, :method
21
21
  # decorator_name :contract
22
22
  def initialize(klass, method, *contracts)
23
+ if contracts[-1].is_a? Hash
24
+ # internally we just convert that return value syntax back to an array
25
+ contracts = contracts[0, contracts.size - 1] + contracts[-1].keys + contracts[-1].values
26
+ else
27
+ fail "It looks like your contract for #{method} doesn't have a return value. A contract should be written as `Contract arg1, arg2 => return_value`."
28
+ end
23
29
  @klass, @method, @contracts = klass, method, contracts
24
30
  end
25
31
 
@@ -115,17 +121,6 @@ class Contract < Decorator
115
121
 
116
122
  def call_with(this, *args, &blk)
117
123
  _args = blk ? args + [blk] : args
118
- if _args.size != @contracts.size - 1
119
- # so it's not *args
120
- unless @contracts.any? { |contract| contract.is_a? Contracts::Args }
121
- raise %{The number of arguments doesn't match the number of contracts.
122
- Did you forget to write a contract for the return value of the function?
123
- Or if you want a variable number of arguments using *args, use the Args contract.
124
- Args: #{args.inspect}
125
- Contracts: #{@contracts.map { |t| t.is_a?(Class) ? t.name : t.class.name }.join(", ")}}
126
- end
127
- end
128
-
129
124
  res = Contract.validate_all(_args, @contracts[0, @contracts.size - 1], @klass, @method)
130
125
  return if res == false
131
126
 
@@ -144,10 +139,8 @@ Contracts: #{@contracts.map { |t| t.is_a?(Class) ? t.name : t.class.name }.join(
144
139
  # class method
145
140
  result = @method.call(*args, &blk)
146
141
  end
147
-
148
- if args.size == @contracts.size - 1
149
- Contract.validate(result, @contracts[-1], @klass, @method, @contracts)
150
- end
142
+
143
+ Contract.validate(result, @contracts[-1], @klass, @method, @contracts)
151
144
  result
152
145
  end
153
146
 
data/lib/foo.rb CHANGED
@@ -1,15 +1,14 @@
1
1
  require 'contracts'
2
2
  use_contracts(self)
3
3
 
4
- add = Proc.new do |x|
5
- 1
4
+ Contract Num, Num => Num
5
+ def add x, y
6
+ x + 1
6
7
  end
7
8
 
8
- Contract ArrayOf[Num], Func[String, Num], ArrayOf[Num]
9
- def map nums, func
10
- nums.map do |num|
11
- func.call(num)
12
- end
9
+ Contract nil => Num
10
+ def test
11
+ 3
13
12
  end
14
13
 
15
- p map([1, 2, 3], add)
14
+ p test
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: 15
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 8
10
- version: 0.0.8
9
+ - 9
10
+ version: 0.0.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Aditya Bhargava