contracts 0.15.0 → 0.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.markdown +6 -0
- data/README.md +2 -2
- data/Rakefile +4 -0
- data/TUTORIAL.md +26 -0
- data/lib/contracts.rb +1 -0
- data/lib/contracts/attrs.rb +20 -0
- data/lib/contracts/builtin_contracts.rb +3 -2
- data/lib/contracts/support.rb +4 -9
- data/lib/contracts/version.rb +1 -1
- data/spec/attrs_spec.rb +75 -0
- data/spec/builtin_contracts_spec.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2050dfd5418498598d0f857e3fe285daa65908c
|
4
|
+
data.tar.gz: 9d5ae5ad04a508233be8ec25b2fb657f2a2b713e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 714992e52ce0d9bdc52c1a0d0ee0c89b0541559e924d2e63e902f73d253c09343f37e8bec1c082ba5bdae76cf146835f09e252f273a617fd5235108b3d2b9f49
|
7
|
+
data.tar.gz: 50518932f7576ddb8a092fa42267321b4502b87a45f96255d2673197075dbdaf9008b3096ce0753cfe57a39a0055c14326dfddeae81ef4ce4243157e55dadd08
|
data/CHANGELOG.markdown
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
## v0.16.0
|
2
|
+
|
3
|
+
- **Support for Ruby 1.8 has been discontinued** - [Corey Farwell](https://github.com/frewsxcv) [#256](https://github.com/egonSchiele/contracts.ruby/pull/256)
|
4
|
+
- Enhancement: Add a `Contracts::Attrs` module containing attribute w/ contracts utilities - [Corey Farwell](https://github.com/frewsxcv) [#255](https://github.com/egonSchiele/contracts.ruby/pull/255)
|
5
|
+
- Bugfix: Fix StrictHash contract for extra keys - [Maciej Malecki](https://github.com/smt116) [#254](https://github.com/egonSchiele/contracts.ruby/pull/254)
|
6
|
+
|
1
7
|
## v0.15.0
|
2
8
|
- Bugfix: Func contract's return value isn't enforced with blocks - [Piotr Szmielew](https://github.com/esse) [#251](https://github.com/egonSchiele/contracts.ruby/pull/251)
|
3
9
|
- Bugfx: Fix contracts used in AR-models - [Gert Goet](https://github.com/eval) [#237](https://github.com/egonSchiele/contracts.ruby/pull/237)
|
data/README.md
CHANGED
@@ -81,7 +81,7 @@ Using contracts.ruby results in very little slowdown. Check out [this blog post]
|
|
81
81
|
|
82
82
|
**Q.** What Rubies can I use this with?
|
83
83
|
|
84
|
-
**A.** It's been tested with `1.
|
84
|
+
**A.** It's been tested with `1.9.2`, `1.9.3`, `2.0.0`, `2.1`, `2.2`, and `jruby` (1.9 mode).
|
85
85
|
|
86
86
|
If you're using the library, please [let me know](https://github.com/egonSchiele) what project you're using it on :)
|
87
87
|
|
@@ -97,7 +97,7 @@ Michael Tomer
|
|
97
97
|
|
98
98
|
## Credits
|
99
99
|
|
100
|
-
Inspired by [contracts.coffee](http://
|
100
|
+
Inspired by [contracts.coffee](http://disnet.github.io/contracts.coffee/).
|
101
101
|
|
102
102
|
Copyright 2012-2015 [Aditya Bhargava](http://adit.io).
|
103
103
|
Major improvements by [Alexey Fedorov](https://github.com/waterlink).
|
data/Rakefile
CHANGED
data/TUTORIAL.md
CHANGED
@@ -606,6 +606,32 @@ Possible validator overrides:
|
|
606
606
|
|
607
607
|
Default validators can be found here: [lib/contracts/validators.rb](https://github.com/egonSchiele/contracts.ruby/blob/master/lib/contracts/validators.rb).
|
608
608
|
|
609
|
+
## Contracts with attributes
|
610
|
+
|
611
|
+
You can include the `Contracts::Attrs` module in your class/module to get access to attribute utilities:
|
612
|
+
|
613
|
+
- `attr_reader_with_contract <symbol>..., <contract>`
|
614
|
+
- Wraps `attr_reader`, validates contract upon 'getting'
|
615
|
+
- `attr_writer_with_contract <symbol>..., <contract>`
|
616
|
+
- Wraps `attr_writer`, validates contract upon 'setting'
|
617
|
+
- `attr_accessor_with_contract <symbol>..., <contract>`
|
618
|
+
- Wraps `attr_accessor`, validates contract upon 'getting' or 'setting'
|
619
|
+
|
620
|
+
### Example
|
621
|
+
|
622
|
+
```ruby
|
623
|
+
class Person
|
624
|
+
include Contracts::Core
|
625
|
+
include Contracts::Attrs
|
626
|
+
|
627
|
+
attr_accessor_with_contract :name, String
|
628
|
+
end
|
629
|
+
|
630
|
+
person = Person.new
|
631
|
+
person.name = 'Jane'
|
632
|
+
person.name = 1.4 # This results in a contract error!
|
633
|
+
```
|
634
|
+
|
609
635
|
## Disabling contracts
|
610
636
|
|
611
637
|
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.
|
data/lib/contracts.rb
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Contracts
|
2
|
+
module Attrs
|
3
|
+
def attr_reader_with_contract(*names, contract)
|
4
|
+
Contract Contracts::None => contract
|
5
|
+
attr_reader(*names)
|
6
|
+
end
|
7
|
+
|
8
|
+
def attr_writer_with_contract(*names, contract)
|
9
|
+
Contract contract => contract
|
10
|
+
attr_writer(*names)
|
11
|
+
end
|
12
|
+
|
13
|
+
def attr_accessor_with_contract(*names, contract)
|
14
|
+
attr_reader_with_contract(*names, contract)
|
15
|
+
attr_writer_with_contract(*names, contract)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
include Attrs
|
20
|
+
end
|
@@ -405,9 +405,10 @@ module Contracts
|
|
405
405
|
|
406
406
|
def valid?(arg)
|
407
407
|
return false unless arg.is_a?(Hash)
|
408
|
+
return false unless arg.keys.sort.eql?(contract_hash.keys.sort)
|
408
409
|
|
409
|
-
contract_hash.all? do |key,
|
410
|
-
contract_hash.key?(key) && Contract.valid?(arg[key],
|
410
|
+
contract_hash.all? do |key, contract|
|
411
|
+
contract_hash.key?(key) && Contract.valid?(arg[key], contract)
|
411
412
|
end
|
412
413
|
end
|
413
414
|
end
|
data/lib/contracts/support.rb
CHANGED
@@ -4,14 +4,10 @@ module Contracts
|
|
4
4
|
def method_position(method)
|
5
5
|
return method.method_position if method.is_a?(MethodReference)
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
else
|
11
|
-
method.inspect
|
12
|
-
end
|
7
|
+
file, line = method.source_location
|
8
|
+
if file.nil? || line.nil?
|
9
|
+
""
|
13
10
|
else
|
14
|
-
file, line = method.source_location
|
15
11
|
file + ":" + line.to_s
|
16
12
|
end
|
17
13
|
end
|
@@ -34,8 +30,7 @@ module Contracts
|
|
34
30
|
end
|
35
31
|
|
36
32
|
def eigenclass_hierarchy_supported?
|
37
|
-
|
38
|
-
RUBY_VERSION.to_f > 1.8
|
33
|
+
RUBY_PLATFORM != "java" || RUBY_VERSION.to_f >= 2.0
|
39
34
|
end
|
40
35
|
|
41
36
|
def eigenclass_of(target)
|
data/lib/contracts/version.rb
CHANGED
data/spec/attrs_spec.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
RSpec.describe "Contracts:" do
|
2
|
+
describe "Attrs:" do
|
3
|
+
class Person
|
4
|
+
include Contracts::Core
|
5
|
+
include Contracts::Attrs
|
6
|
+
include Contracts::Builtin
|
7
|
+
|
8
|
+
def initialize(name)
|
9
|
+
@name_r = name
|
10
|
+
@name_w = name
|
11
|
+
@name_rw = name
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader_with_contract :name_r, String
|
15
|
+
attr_writer_with_contract :name_w, String
|
16
|
+
attr_accessor_with_contract :name_rw, String
|
17
|
+
end
|
18
|
+
|
19
|
+
context "attr_reader_with_contract" do
|
20
|
+
it "getting valid type" do
|
21
|
+
expect(Person.new("bob").name_r)
|
22
|
+
.to(eq("bob"))
|
23
|
+
end
|
24
|
+
|
25
|
+
it "getting invalid type" do
|
26
|
+
expect { Person.new(1.3).name_r }
|
27
|
+
.to(raise_error(ReturnContractError))
|
28
|
+
end
|
29
|
+
|
30
|
+
it "setting" do
|
31
|
+
expect { Person.new("bob").name_r = "alice" }
|
32
|
+
.to(raise_error(NoMethodError))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "attr_writer_with_contract" do
|
37
|
+
it "getting" do
|
38
|
+
expect { Person.new("bob").name_w }
|
39
|
+
.to(raise_error(NoMethodError))
|
40
|
+
end
|
41
|
+
|
42
|
+
it "setting valid type" do
|
43
|
+
expect(Person.new("bob").name_w = "alice")
|
44
|
+
.to(eq("alice"))
|
45
|
+
end
|
46
|
+
|
47
|
+
it "setting invalid type" do
|
48
|
+
expect { Person.new("bob").name_w = 1.2 }
|
49
|
+
.to(raise_error(ParamContractError))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "attr_accessor_with_contract" do
|
54
|
+
it "getting valid type" do
|
55
|
+
expect(Person.new("bob").name_rw)
|
56
|
+
.to(eq("bob"))
|
57
|
+
end
|
58
|
+
|
59
|
+
it "getting invalid type" do
|
60
|
+
expect { Person.new(1.2).name_rw }
|
61
|
+
.to(raise_error(ReturnContractError))
|
62
|
+
end
|
63
|
+
|
64
|
+
it "setting valid type" do
|
65
|
+
expect(Person.new("bob").name_rw = "alice")
|
66
|
+
.to(eq("alice"))
|
67
|
+
end
|
68
|
+
|
69
|
+
it "setting invalid type" do
|
70
|
+
expect { Person.new("bob").name_rw = 1.2 }
|
71
|
+
.to(raise_error(ParamContractError))
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -448,7 +448,7 @@ RSpec.describe "Contracts:" do
|
|
448
448
|
|
449
449
|
context "when given an input with extra keys" do
|
450
450
|
it "raises an error" do
|
451
|
-
fails { @o.strict_person(:name => "calvin", :age =>
|
451
|
+
fails { @o.strict_person(:name => "calvin", :age => 10, :soft => true) }
|
452
452
|
end
|
453
453
|
end
|
454
454
|
|
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.16.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aditya Bhargava
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-04-24 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
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- features/builtin_contracts/xor.feature
|
69
69
|
- features/support/env.rb
|
70
70
|
- lib/contracts.rb
|
71
|
+
- lib/contracts/attrs.rb
|
71
72
|
- lib/contracts/builtin_contracts.rb
|
72
73
|
- lib/contracts/call_with.rb
|
73
74
|
- lib/contracts/core.rb
|
@@ -88,6 +89,7 @@ files:
|
|
88
89
|
- script/docs-release
|
89
90
|
- script/docs-staging
|
90
91
|
- script/rubocop.rb
|
92
|
+
- spec/attrs_spec.rb
|
91
93
|
- spec/builtin_contracts_spec.rb
|
92
94
|
- spec/contracts_spec.rb
|
93
95
|
- spec/fixtures/fixtures.rb
|