contracts 0.6 → 0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/TUTORIAL.md +13 -10
- data/lib/contracts/eigenclass.rb +1 -0
- data/lib/contracts/method_reference.rb +2 -1
- data/lib/contracts/version.rb +1 -1
- data/spec/contracts_spec.rb +12 -0
- data/spec/fixtures/fixtures.rb +12 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 065efb039a13c2917e10fc96dd77dd2fe858bc08
|
4
|
+
data.tar.gz: 98f3253cc22a5da08f4d3414361db7218c438107
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8486b6e85a38517371403aa4cb7b3e6795ee01a2170e663eab8ff772507971a8e7bfa050b3b98dabb8fc60772153f544a14e9e419025a7c899ebda682b82338
|
7
|
+
data.tar.gz: 44cfdb1101dbe48269e88c5758196e2bc11ce2238f70a791c0f62aa36d0754994dd00f49f4c8e0d98cca43a4b254a085cc83af3673964b6e31198ba66c14527e
|
data/Gemfile.lock
CHANGED
data/TUTORIAL.md
CHANGED
@@ -29,14 +29,17 @@ Copy this code into a file and run it:
|
|
29
29
|
|
30
30
|
```ruby
|
31
31
|
require 'contracts'
|
32
|
-
include Contracts
|
33
32
|
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
class Math
|
34
|
+
include Contracts
|
35
|
+
|
36
|
+
Contract Num, Num => Num
|
37
|
+
def self.add(a, b)
|
38
|
+
a + b
|
39
|
+
end
|
37
40
|
end
|
38
41
|
|
39
|
-
puts add(1, "foo")
|
42
|
+
puts Math.add(1, "foo")
|
40
43
|
```
|
41
44
|
|
42
45
|
You'll see a detailed error message like so:
|
@@ -60,7 +63,7 @@ This can be useful if you're in a repl and want to figure out how a function sho
|
|
60
63
|
|
61
64
|
## Builtin Contracts
|
62
65
|
|
63
|
-
`Num` is one of the builtin contracts that contracts.ruby comes with. The builtin contracts are in the `Contracts` namespace. The easiest way to use them is to
|
66
|
+
`Num` is one of the builtin contracts that contracts.ruby comes with. The builtin contracts are in the `Contracts` namespace. The easiest way to use them is to include the `Contracts` module in your class/module.
|
64
67
|
|
65
68
|
contracts.ruby comes with a lot of builtin contracts, including:
|
66
69
|
|
@@ -401,7 +404,7 @@ If your failure callback returns `false`, the method that the contract is guardi
|
|
401
404
|
|
402
405
|
## Disabling contracts
|
403
406
|
|
404
|
-
If you want to disable contracts, set the `NO_CONTRACTS` environment variable. This will disable contracts
|
407
|
+
If you want to disable contracts, set the `NO_CONTRACTS` environment variable. This will disable contracts and you won't have a performance hit. Pattern matching will still work if you disable contracts in this way! With NO_CONTRACTS only pattern-matching contracts are defined.
|
405
408
|
|
406
409
|
## Method overloading
|
407
410
|
|
@@ -439,12 +442,12 @@ For an argument, each function will be tried in order. The first function that d
|
|
439
442
|
This allows you write methods more declaratively, rather than using conditional branching. This feature is not only useful for recursion; you can use it to keep parallel use cases separate:
|
440
443
|
|
441
444
|
```ruby
|
442
|
-
Contract
|
445
|
+
Contract lambda{|n| n < 12 } => Ticket
|
443
446
|
def get_ticket(age)
|
444
447
|
ChildTicket.new(age: age)
|
445
448
|
end
|
446
449
|
|
447
|
-
Contract
|
450
|
+
Contract lambda{|n| n >= 12 } => Ticket
|
448
451
|
def get_ticket(age)
|
449
452
|
AdultTicket.new(age: age)
|
450
453
|
end
|
@@ -486,7 +489,7 @@ A simple example:
|
|
486
489
|
```ruby
|
487
490
|
class MyBirthday < Struct.new(:day, :month)
|
488
491
|
include Contracts
|
489
|
-
include Contracts
|
492
|
+
include Contracts::Invariants
|
490
493
|
|
491
494
|
Invariant(:day) { 1 <= day && day <= 31 }
|
492
495
|
Invariant(:month) { 1 <= month && month <= 12 }
|
data/lib/contracts/eigenclass.rb
CHANGED
@@ -24,7 +24,8 @@ module Contracts
|
|
24
24
|
|
25
25
|
# Makes a method private
|
26
26
|
def make_private(this)
|
27
|
-
|
27
|
+
original_name = name
|
28
|
+
alias_target(this).class_eval { private original_name }
|
28
29
|
end
|
29
30
|
|
30
31
|
# Aliases original method to a special unique name, which is known
|
data/lib/contracts/version.rb
CHANGED
data/spec/contracts_spec.rb
CHANGED
@@ -107,6 +107,14 @@ RSpec.describe "Contracts:" do
|
|
107
107
|
}.to raise_error(*error)
|
108
108
|
end
|
109
109
|
end
|
110
|
+
|
111
|
+
describe "builtin contracts usage" do
|
112
|
+
it "allows to use builtin contracts without namespacing and redundant Contracts inclusion" do
|
113
|
+
expect {
|
114
|
+
SingletonClassExample.add("55", 5.6)
|
115
|
+
}.to raise_error(ContractError, /Expected: Contracts::Num/)
|
116
|
+
end
|
117
|
+
end
|
110
118
|
end
|
111
119
|
|
112
120
|
describe "no contracts feature" do
|
@@ -449,6 +457,10 @@ RSpec.describe "Contracts:" do
|
|
449
457
|
it "should raise an error if you try to access a private method" do
|
450
458
|
expect { @o.a_private_method }.to raise_error
|
451
459
|
end
|
460
|
+
|
461
|
+
it "should raise an error if you try to access a private method" do
|
462
|
+
expect { @o.a_really_private_method }.to raise_error
|
463
|
+
end
|
452
464
|
end
|
453
465
|
|
454
466
|
describe "inherited methods" do
|
data/spec/fixtures/fixtures.rb
CHANGED
@@ -214,6 +214,13 @@ class GenericExample
|
|
214
214
|
end
|
215
215
|
private :a_private_method
|
216
216
|
|
217
|
+
private
|
218
|
+
|
219
|
+
Contract nil => String
|
220
|
+
def a_really_private_method
|
221
|
+
"works for sure"
|
222
|
+
end
|
223
|
+
|
217
224
|
end
|
218
225
|
|
219
226
|
# for testing inheritance
|
@@ -323,6 +330,11 @@ class SingletonClassExample
|
|
323
330
|
def hoge(str)
|
324
331
|
"super#{str}"
|
325
332
|
end
|
333
|
+
|
334
|
+
Contract Num, Num => Num
|
335
|
+
def add(a, b)
|
336
|
+
a + b
|
337
|
+
end
|
326
338
|
end
|
327
339
|
end
|
328
340
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contracts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.7'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aditya Bhargava
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: This library provides contracts for Ruby. Contracts let you clearly express
|
14
14
|
how your code behaves, and free you from writing tons of boilerplate, defensive
|