contracts 0.12.0 → 0.16.1
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.
- checksums.yaml +5 -13
- data/.github/workflows/code_style_checks.yaml +36 -0
- data/.github/workflows/tests.yaml +47 -0
- data/CHANGELOG.markdown +57 -6
- data/Gemfile +2 -2
- data/LICENSE +23 -0
- data/README.md +14 -6
- data/Rakefile +3 -6
- data/TUTORIAL.md +55 -26
- data/contracts.gemspec +6 -1
- data/dependabot.yml +20 -0
- data/features/basics/pretty-print.feature +241 -0
- data/features/builtin_contracts/args.feature +80 -1
- data/features/builtin_contracts/int.feature +93 -0
- data/features/builtin_contracts/nat_pos.feature +119 -0
- data/lib/contracts.rb +48 -12
- data/lib/contracts/attrs.rb +24 -0
- data/lib/contracts/builtin_contracts.rb +60 -1
- data/lib/contracts/call_with.rb +22 -11
- data/lib/contracts/core.rb +0 -2
- data/lib/contracts/decorators.rb +5 -0
- data/lib/contracts/engine/eigenclass.rb +4 -0
- data/lib/contracts/engine/target.rb +2 -0
- data/lib/contracts/formatters.rb +4 -2
- data/lib/contracts/method_handler.rb +14 -22
- data/lib/contracts/support.rb +11 -9
- data/lib/contracts/version.rb +1 -1
- data/spec/attrs_spec.rb +119 -0
- data/spec/builtin_contracts_spec.rb +157 -95
- data/spec/contracts_spec.rb +50 -29
- data/spec/fixtures/fixtures.rb +58 -0
- data/spec/methods_spec.rb +54 -0
- data/spec/override_validators_spec.rb +1 -1
- data/spec/ruby_version_specific/contracts_spec_2.0.rb +15 -0
- data/spec/ruby_version_specific/contracts_spec_2.1.rb +1 -1
- data/spec/validators_spec.rb +1 -1
- metadata +25 -14
- data/script/cucumber +0 -5
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
NDFhYTJmODVkY2FlNWE3ZGI2ZjcxM2JiODMyODBlZjM4MmRjZDkwZQ==
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0f008c5c8ab0f0e8d60c14f29413e2b6b262f53b53d9bdc34a618d438a3065a2
|
4
|
+
data.tar.gz: 812d80e1696cfe92350c4402d5a2368589d7ecca4b1849f6ca110a8afe239d18
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
ZDAwMmNhZWVkMTMxMDZkZTUwOGMwN2VhYjBlOTg2MGFhNmEzNjM1N2Q0NTJi
|
11
|
-
YzMzN2Y1ZTBiMWIwZDgyYWM0MmM5N2IyMjQ3ZWVmZGMwYzQxZDE=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
ZTI2OWM5ZTNmYjFmMTI1NjU5ODUwNzM4MTg3NDBjYjkxM2RkZDJiYTBjNTcy
|
14
|
-
ZGY4NzYzZWU2NmM3Njc2ZmFlZjAzYzBkZDdhYTQ3ZmViNDYyMDY5Yzc2N2M3
|
15
|
-
Y2U5NTczYjdiODZjZjAxMzNiOGVjYTkyNzlhY2UyYmM3NDQzOWQ=
|
6
|
+
metadata.gz: 37e8c58c78748244de26e797301d7510a16263a1373a45b69b01749f34d8a0990c60b9ec02adf49b745abf1493911e61596b90d37af60663abd84e8f5c5273d5
|
7
|
+
data.tar.gz: 502421a171fc8284295c9253edb405321c05ef871d0aafb52b80e9b7cf5099f216b7f71ca3dce696e582854feedaad80faa00394a0fb903760fa732c34cd87a0
|
@@ -0,0 +1,36 @@
|
|
1
|
+
name: Code Style Checks
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
paths-ignore:
|
8
|
+
- 'README.md'
|
9
|
+
push:
|
10
|
+
branches:
|
11
|
+
- master
|
12
|
+
paths-ignore:
|
13
|
+
- 'README.md'
|
14
|
+
|
15
|
+
jobs:
|
16
|
+
rubocop:
|
17
|
+
name: Rubocop
|
18
|
+
if: "contains(github.event.commits[0].message, '[ci skip]') == false"
|
19
|
+
strategy:
|
20
|
+
fail-fast: false
|
21
|
+
matrix:
|
22
|
+
os:
|
23
|
+
- ubuntu
|
24
|
+
ruby:
|
25
|
+
- "2.7"
|
26
|
+
runs-on: ${{ matrix.os }}-latest
|
27
|
+
steps:
|
28
|
+
- name: Checkout
|
29
|
+
uses: actions/checkout@v2
|
30
|
+
- name: Setup Ruby
|
31
|
+
uses: ruby/setup-ruby@v1
|
32
|
+
with:
|
33
|
+
ruby-version: ${{ matrix.ruby }}
|
34
|
+
bundler-cache: true
|
35
|
+
- name: Run Rubocop
|
36
|
+
run: bundle exec rubocop
|
@@ -0,0 +1,47 @@
|
|
1
|
+
name: Tests
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
paths-ignore:
|
8
|
+
- 'README.md'
|
9
|
+
push:
|
10
|
+
branches:
|
11
|
+
- master
|
12
|
+
paths-ignore:
|
13
|
+
- 'README.md'
|
14
|
+
|
15
|
+
jobs:
|
16
|
+
unit_tests:
|
17
|
+
name: Unit Tests
|
18
|
+
if: "contains(github.event.commits[0].message, '[ci skip]') == false"
|
19
|
+
strategy:
|
20
|
+
fail-fast: false
|
21
|
+
matrix:
|
22
|
+
os:
|
23
|
+
- ubuntu
|
24
|
+
ruby:
|
25
|
+
- "2.1"
|
26
|
+
- "2.2"
|
27
|
+
- "2.3"
|
28
|
+
- "2.4"
|
29
|
+
- "2.5"
|
30
|
+
- "2.6"
|
31
|
+
- "2.7"
|
32
|
+
test_command: ["bundle exec rspec && bundle exec cucumber"]
|
33
|
+
include:
|
34
|
+
- os: ubuntu
|
35
|
+
ruby: "2.4.2"
|
36
|
+
test_command: "bundle exec rspec"
|
37
|
+
runs-on: ${{ matrix.os }}-latest
|
38
|
+
steps:
|
39
|
+
- name: Checkout
|
40
|
+
uses: actions/checkout@v2
|
41
|
+
- name: Setup Ruby
|
42
|
+
uses: ruby/setup-ruby@v1
|
43
|
+
with:
|
44
|
+
ruby-version: ${{ matrix.ruby }}
|
45
|
+
bundler-cache: true
|
46
|
+
- name: Test
|
47
|
+
run: ${{ matrix.test_command }}
|
data/CHANGELOG.markdown
CHANGED
@@ -1,11 +1,56 @@
|
|
1
|
-
|
1
|
+
|
2
|
+
## [v0.16.1] - 2021-04-17
|
3
|
+
|
4
|
+
- Enhancement: Pretty-print contracts in error messages - [Corey Farwell](https://github.com/frewsxcv) [#289](https://github.com/egonSchiele/contracts.ruby/pull/289)
|
5
|
+
- Bugfix: Fix `attr_accessor_with_contract` with multiple attribute names input - [Kevin Yeh](https://github.com/kyeah) [#259](https://github.com/egonSchiele/contracts.ruby/pull/259)
|
6
|
+
- Bugfix: Fix "stack level too deep" in CI builds - [md-work](https://github.com/md-work) [#283](https://github.com/egonSchiele/contracts.ruby/pull/283)
|
7
|
+
|
8
|
+
## [v0.16.0] - 2017-04-24
|
9
|
+
|
10
|
+
[v0.16.0]: https://github.com/egonSchiele/contracts.ruby/compare/v0.15.0...v0.16.0
|
11
|
+
|
12
|
+
- **Support for Ruby 1.8 has been discontinued** - [Corey Farwell](https://github.com/frewsxcv) [#256](https://github.com/egonSchiele/contracts.ruby/pull/256)
|
13
|
+
- 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)
|
14
|
+
- Bugfix: Fix StrictHash contract for extra keys - [Maciej Malecki](https://github.com/smt116) [#254](https://github.com/egonSchiele/contracts.ruby/pull/254)
|
15
|
+
|
16
|
+
## [v0.15.0] - 2017-02-24
|
17
|
+
|
18
|
+
[v0.15.0]: https://github.com/egonSchiele/contracts.ruby/compare/v0.14.0...v0.15.0
|
19
|
+
|
20
|
+
- 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)
|
21
|
+
- Bugfx: Fix contracts used in AR-models - [Gert Goet](https://github.com/eval) [#237](https://github.com/egonSchiele/contracts.ruby/pull/237)
|
22
|
+
|
23
|
+
## [v0.14.0] - 2016-04-26
|
24
|
+
|
25
|
+
[v0.14.0]: https://github.com/egonSchiele/contracts.ruby/compare/v0.13.0...v0.14.0
|
26
|
+
|
27
|
+
- Enhancement: Add StrictHash contract - [Fyodor](https://github.com/cbrwizard) [#236](https://github.com/egonSchiele/contracts.ruby/pull/236)
|
28
|
+
- Bugfix: dont fail if something other than a hash is passed to a KeywordArgs - [Dan Padilha](https://github.com/dpad) [#234](https://github.com/egonSchiele/contracts.ruby/pull/234)
|
29
|
+
- LICENSE ADDED: Simplified BSD (same as what is specified in the readme) - [Charles Dale](https://github.com/chuckd) [#233](https://github.com/egonSchiele/contracts.ruby/pull/233)
|
30
|
+
- Bugfix: fix constant looking when including a module that includes contracts (requires removing the check to see if contracts is already included) - [Aditya Bhargava](https://github.com/egonSchiele) [#232](https://github.com/egonSchiele/contracts.ruby/pull/232)
|
31
|
+
- Bugfix for err case when KeywordArgs and Proc are used together - [Aditya Bhargava](https://github.com/egonSchiele) [#230](https://github.com/egonSchiele/contracts.ruby/pull/230)
|
32
|
+
- Enhancement: Add DescendantOf contract - [Miguel Palhas](https://github.com/naps62) [#227](https://github.com/egonSchiele/contracts.ruby/pull/227)
|
33
|
+
|
34
|
+
## [v0.13.0] - 2016-01-25
|
35
|
+
|
36
|
+
[v0.13.0]: https://github.com/egonSchiele/contracts.ruby/compare/v0.12.0...v0.13.0
|
37
|
+
|
38
|
+
- Enhancement: Add support for Ruby 2.3 - [Oleksii Fedorov](https://github.com/waterlink) [#216](https://github.com/egonSchiele/contracts.ruby/pull/216)
|
39
|
+
- Enhancement: Added Int, Nat and NatPos builtin contracts - [Simon George](https://github.com/sfcgeorge) [#212](https://github.com/egonSchiele/contracts.ruby/pull/212)
|
40
|
+
- Bugfix: Allow contracts on singleton of subclass - [Oleksii Federov](https://github.com/waterlink) [#211](https://github.com/egonSchiele/contracts.ruby/pull/211)
|
41
|
+
|
42
|
+
## [v0.12.0] - 2015-09-15
|
43
|
+
|
44
|
+
[v0.12.0]: https://github.com/egonSchiele/contracts.ruby/compare/v0.11.0...v0.12.0
|
2
45
|
|
3
46
|
- Feature: add `Regexp` validator - [Gert Goet](https://github.com/eval) [#196](https://github.com/egonSchiele/contracts.ruby/pull/196)
|
4
47
|
- Docs: bootstrap cucumber/aruba/relish setup - [Oleksii Fedorov](https://github.com/waterlink) [#195](https://github.com/egonSchiele/contracts.ruby/pull/195)
|
5
48
|
- Bugfix: allow to `extend` module, that has `Contracts` or `Contracts::Core` included without harming current module/class `Contracts` functionality, see: [#176](https://github.com/egonSchiele/contracts.ruby/issues/176) - [Oleksii Fedorov](https://github.com/waterlink) [#198](https://github.com/egonSchiele/contracts.ruby/pull/198)
|
6
49
|
- Enhancement: add `include Contracts::Builtin` to allow users to use builtin contracts without `Contracts::` prefix together with `include Contracts::Core` - [PikachuEXE](https://github.com/PikachuEXE) [#199](https://github.com/egonSchiele/contracts.ruby/pull/199)
|
7
50
|
|
8
|
-
## v0.11.0
|
51
|
+
## [v0.11.0] - 2015-07-30
|
52
|
+
|
53
|
+
[v0.11.0]: https://github.com/egonSchiele/contracts.ruby/compare/v0.10.1...v0.11.0
|
9
54
|
|
10
55
|
- Enhancement: add `include Contracts::Core` that doesn't pollute the namespace as much as `include Contracts` - [Oleksii Federov](https://github.com/waterlink) [#185](https://github.com/egonSchiele/contracts.ruby/pull/185)
|
11
56
|
- Bugfix: fail if a non-hash is provided to a `HashOf` contract - [Abe Voelker](https://github.com/abevoelker) [#190](https://github.com/egonSchiele/contracts.ruby/pull/190)
|
@@ -14,11 +59,15 @@
|
|
14
59
|
- Feature: range contract added - [Oleksii Fedorov](https://github.com/waterlink) [#184](https://github.com/egonSchiele/contracts.ruby/pull/184)
|
15
60
|
- Feature: enum contract added - [Dennis Günnewig](https://github.com/dg-ratiodata) [#181](https://github.com/egonSchiele/contracts.ruby/pull/181)
|
16
61
|
|
17
|
-
## v0.10.1
|
62
|
+
## [v0.10.1] - 2015-07-16
|
63
|
+
|
64
|
+
[v0.10.1]: https://github.com/egonSchiele/contracts.ruby/compare/v0.10...v0.10.1
|
18
65
|
|
19
66
|
- Enhancement: make `@pattern_match` instance variable not render ruby warning. Required to use new aruba versions in rspec tests - [Dennis Günnewig](https://github.com/dg-ratiodata) [#179](https://github.com/egonSchiele/contracts.ruby/pull/179)
|
20
67
|
|
21
|
-
## v0.10
|
68
|
+
## [v0.10] - 2015-07-07
|
69
|
+
|
70
|
+
[v0.10]: https://github.com/egonSchiele/contracts.ruby/compare/v0.9...v0.10
|
22
71
|
|
23
72
|
- Bugfix: make `Maybe[Proc]` work correctly - [Simon George](https://github.com/sfcgeorge) [#142](https://github.com/egonSchiele/contracts.ruby/pull/142)
|
24
73
|
- Bugfix: make `Func` contract verified when used as return contract - [Rob Rosenbaum](https://github.com/robnormal) [#145](https://github.com/egonSchiele/contracts.ruby/pull/145)
|
@@ -29,7 +78,9 @@
|
|
29
78
|
- Feature: custom validators with `Contract.override_validator` - [Oleksii Fedorov](https://github.com/waterlink) [#159](https://github.com/egonSchiele/contracts.ruby/pull/159)
|
30
79
|
- Feature: add builtin `RangeOf[...]` contract - [Gavin Sinclair](https://github.com/gsinclair) [#171](https://github.com/egonSchiele/contracts.ruby/pull/171)
|
31
80
|
|
32
|
-
## v0.9
|
81
|
+
## [v0.9] - 2015-04-24
|
82
|
+
|
83
|
+
[v0.9]: https://github.com/egonSchiele/contracts.ruby/compare/0.8...v0.9
|
33
84
|
|
34
85
|
- MAJOR fix in pattern-matching: If the return contract for a pattern-matched function fails, it should NOT try the next pattern-match function. Pattern-matching is only for params, not return values.
|
35
86
|
- raise an error if multiple defns have the same contract for pattern matching.
|
@@ -50,7 +101,7 @@
|
|
50
101
|
- Add `SetOf` contract
|
51
102
|
- various small fixes
|
52
103
|
|
53
|
-
## v0.8
|
104
|
+
## v0.8 - 2015-04-03
|
54
105
|
|
55
106
|
- code refactored (very slight loss of performance, big increase in readability)
|
56
107
|
- fail when defining a contract on a module without `include Contracts::Modules`
|
data/Gemfile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source "
|
1
|
+
source "https://rubygems.org"
|
2
2
|
|
3
3
|
gemspec
|
4
4
|
|
@@ -6,7 +6,7 @@ group :test do
|
|
6
6
|
gem "rspec"
|
7
7
|
gem "aruba"
|
8
8
|
gem "cucumber", "~> 1.3.20"
|
9
|
-
gem "rubocop", "~> 0.
|
9
|
+
gem "rubocop", "~> 0.41.2" if RUBY_VERSION >= "2"
|
10
10
|
end
|
11
11
|
|
12
12
|
group :development do
|
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2012-2016 Aditya Bhargava
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
15
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
17
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
18
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
20
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
21
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
22
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
23
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
This project is looking for a new maintainer! [More details here](https://github.com/egonSchiele/contracts.ruby/issues/249)
|
2
|
+
|
3
|
+
# contracts.ruby [](https://github.com/egonSchiele/contracts.ruby/actions?query=workflow%3ATests) [](https://gitter.im/egonSchiele/contracts.ruby)
|
2
4
|
|
3
5
|
Contracts let you clearly – even beautifully – express how your code behaves, and free you from writing tons of boilerplate, defensive code.
|
4
6
|
|
@@ -39,6 +41,7 @@ puts Example.new.double("oops")
|
|
39
41
|
|
40
42
|
Save this in a file and run it. Notice we are calling `double` with `"oops"`, which is not a number. The contract fails with a detailed error message:
|
41
43
|
|
44
|
+
```
|
42
45
|
ParamContractError: Contract violation for argument 1 of 1:
|
43
46
|
Expected: Num,
|
44
47
|
Actual: "oops"
|
@@ -46,12 +49,13 @@ ParamContractError: Contract violation for argument 1 of 1:
|
|
46
49
|
With Contract: Num => Num
|
47
50
|
At: main.rb:8
|
48
51
|
...stack trace...
|
52
|
+
```
|
49
53
|
|
50
54
|
Instead of throwing an exception, you could log it, print a clean error message for your user...whatever you want. contracts.ruby is here to help you handle bugs better, not to get in your way.
|
51
55
|
|
52
56
|
## Tutorial
|
53
57
|
|
54
|
-
Check out [this awesome tutorial](
|
58
|
+
Check out [this awesome tutorial](https://egonschiele.github.io/contracts.ruby/).
|
55
59
|
|
56
60
|
## Use Cases
|
57
61
|
|
@@ -67,15 +71,19 @@ To get started do the following:
|
|
67
71
|
|
68
72
|
2. Run our test suite
|
69
73
|
|
70
|
-
`bundle exec
|
71
|
-
|
74
|
+
`bundle exec rspec`
|
75
|
+
|
76
|
+
3. Run our code style checks
|
77
|
+
|
78
|
+
`bundle exec rubocop`
|
79
|
+
|
72
80
|
## Performance
|
73
81
|
|
74
82
|
Using contracts.ruby results in very little slowdown. Check out [this blog post](http://adit.io/posts/2013-03-04-How-I-Made-My-Ruby-Project-10x-Faster.html#seconds-6) for more info.
|
75
83
|
|
76
84
|
**Q.** What Rubies can I use this with?
|
77
85
|
|
78
|
-
**A.** It's been tested with `1
|
86
|
+
**A.** It's been tested with `2.1`, `2.2`, `2.3`, `2.4`, `2.5`, `2.6` and `2.7`.
|
79
87
|
|
80
88
|
If you're using the library, please [let me know](https://github.com/egonSchiele) what project you're using it on :)
|
81
89
|
|
@@ -91,7 +99,7 @@ Michael Tomer
|
|
91
99
|
|
92
100
|
## Credits
|
93
101
|
|
94
|
-
Inspired by [contracts.coffee](http://
|
102
|
+
Inspired by [contracts.coffee](http://disnet.github.io/contracts.coffee/).
|
95
103
|
|
96
104
|
Copyright 2012-2015 [Aditya Bhargava](http://adit.io).
|
97
105
|
Major improvements by [Alexey Fedorov](https://github.com/waterlink).
|
data/Rakefile
CHANGED
@@ -1,10 +1,7 @@
|
|
1
|
-
|
2
|
-
task :default => [:spec, :rubocop]
|
1
|
+
task :default => [:spec]
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
else
|
7
|
-
task :default => [:spec]
|
3
|
+
task :add_tag do
|
4
|
+
`git tag -a v#{Contracts::VERSION} -m 'v#{Contracts::VERSION}'`
|
8
5
|
end
|
9
6
|
|
10
7
|
require "rspec/core/rake_task"
|
data/TUTORIAL.md
CHANGED
@@ -68,42 +68,45 @@ This can be useful if you're in a REPL and want to figure out how a function sho
|
|
68
68
|
contracts.ruby comes with a lot of built-in contracts, including the following:
|
69
69
|
|
70
70
|
* Basic types
|
71
|
-
* [`Num`](http://www.rubydoc.info/gems/contracts/Contracts/Num) – checks that the argument is `Numeric`
|
72
|
-
* [`Pos`](http://www.rubydoc.info/gems/contracts/Contracts/Pos) – checks that the argument is a positive number
|
73
|
-
* [`Neg`](http://www.rubydoc.info/gems/contracts/Contracts/Neg) – checks that the argument is a negative number
|
74
|
-
* [`
|
75
|
-
* [`
|
76
|
-
* [`
|
77
|
-
* [`
|
71
|
+
* [`Num`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Num) – checks that the argument is `Numeric`
|
72
|
+
* [`Pos`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Pos) – checks that the argument is a positive number
|
73
|
+
* [`Neg`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Neg) – checks that the argument is a negative number
|
74
|
+
* [`Int`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Int) – checks that the argument is an integer
|
75
|
+
* [`Nat`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Nat) – checks that the argument is a natural number (>= 0)
|
76
|
+
* [`NatPos`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/NatPos) – checks that the argument is a positive natural number (> 0)
|
77
|
+
* [`Bool`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Bool) – checks that the argument is `true` or `false`
|
78
|
+
* [`Any`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Any) – Passes for any argument. Use when the argument has no constraints.
|
79
|
+
* [`None`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/None) – Fails for any argument. Use when the method takes no arguments.
|
78
80
|
|
79
81
|
* Logical combinations
|
80
|
-
* [`Maybe`](http://www.rubydoc.info/gems/contracts/Contracts/Maybe) – specifies that a value _may be_ nil, e.g. `Maybe[String]` (equivalent to `Or[String,nil]`)
|
81
|
-
* [`Or`](http://www.rubydoc.info/gems/contracts/Contracts/Or) – passes if any of the given contracts pass, e.g. `Or[Fixnum, Float]`
|
82
|
-
* [`Xor`](http://www.rubydoc.info/gems/contracts/Contracts/Xor) – passes if exactly one of the given contracts pass, e.g. `Xor[Fixnum, Float]`
|
83
|
-
* [`And`](http://www.rubydoc.info/gems/contracts/Contracts/And) – passes if all contracts pass, e.g. `And[Nat, -> (n) { n.even? }]`
|
84
|
-
* [`Not`](http://www.rubydoc.info/gems/contracts/Contracts/Not) – passes if all contracts fail for the given argument, e.g. `Not[nil]`
|
82
|
+
* [`Maybe`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Maybe) – specifies that a value _may be_ nil, e.g. `Maybe[String]` (equivalent to `Or[String,nil]`)
|
83
|
+
* [`Or`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Or) – passes if any of the given contracts pass, e.g. `Or[Fixnum, Float]`
|
84
|
+
* [`Xor`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Xor) – passes if exactly one of the given contracts pass, e.g. `Xor[Fixnum, Float]`
|
85
|
+
* [`And`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/And) – passes if all contracts pass, e.g. `And[Nat, -> (n) { n.even? }]`
|
86
|
+
* [`Not`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Not) – passes if all contracts fail for the given argument, e.g. `Not[nil]`
|
85
87
|
|
86
88
|
* Collections
|
87
|
-
* [`ArrayOf`](http://www.rubydoc.info/gems/contracts/Contracts/ArrayOf) – checks that the argument is an array, and all elements pass the given contract, e.g. `ArrayOf[Num]`
|
88
|
-
* [`SetOf`](http://www.rubydoc.info/gems/contracts/Contracts/SetOf) – checks that the argument is a set, and all elements pass the given contract, e.g. `SetOf[Num]`
|
89
|
-
* [`HashOf`](http://www.rubydoc.info/gems/contracts/Contracts/HashOf) – checks that the argument is a hash, and all keys and values pass the given contract, e.g. `HashOf[Symbol => String]` or `HashOf[Symbol,String]`
|
90
|
-
* [`
|
91
|
-
* [`
|
89
|
+
* [`ArrayOf`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/ArrayOf) – checks that the argument is an array, and all elements pass the given contract, e.g. `ArrayOf[Num]`
|
90
|
+
* [`SetOf`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/SetOf) – checks that the argument is a set, and all elements pass the given contract, e.g. `SetOf[Num]`
|
91
|
+
* [`HashOf`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/HashOf) – checks that the argument is a hash, and all keys and values pass the given contract, e.g. `HashOf[Symbol => String]` or `HashOf[Symbol,String]`
|
92
|
+
* [`StrictHash`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/StrictHash) – checks that the argument is a hash, and every key passed is present in the given contract, e.g. `StrictHash[{ :description => String, :number => Fixnum }]`
|
93
|
+
* [`RangeOf`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/RangeOf) – checks that the argument is a range whose elements (#first and #last) pass the given contract, e.g. `RangeOf[Date]`
|
94
|
+
* [`Enum`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Enum) – checks that the argument is part of a given collection of objects, e.g. `Enum[:a, :b, :c]`
|
92
95
|
|
93
96
|
* Keyword arguments
|
94
|
-
* [`KeywordArgs`](http://www.rubydoc.info/gems/contracts/Contracts/KeywordArgs) – checks that the argument is an options hash, and all required keyword arguments are present, and all values pass their respective contracts, e.g. `KeywordArgs[:number => Num, :description => Optional[String]]`
|
95
|
-
* [`Optional`](http://www.rubydoc.info/gems/contracts/Contracts/Optional) – checks that the keyword argument is either not present or pass the given contract, can not be used outside of `KeywordArgs` contract, e.g. `Optional[Num]`
|
97
|
+
* [`KeywordArgs`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/KeywordArgs) – checks that the argument is an options hash, and all required keyword arguments are present, and all values pass their respective contracts, e.g. `KeywordArgs[:number => Num, :description => Optional[String]]`
|
98
|
+
* [`Optional`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Optional) – checks that the keyword argument is either not present or pass the given contract, can not be used outside of `KeywordArgs` contract, e.g. `Optional[Num]`
|
96
99
|
|
97
100
|
* Duck typing
|
98
|
-
* [`RespondTo`](http://www.rubydoc.info/gems/contracts/Contracts/RespondTo) – checks that the argument responds to all of the given methods, e.g. `RespondTo[:password, :credit_card]`
|
99
|
-
* [`Send`](http://www.rubydoc.info/gems/contracts/Contracts/Send) – checks that all named methods return a truthy value, e.g. `Send[:valid?]`
|
101
|
+
* [`RespondTo`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/RespondTo) – checks that the argument responds to all of the given methods, e.g. `RespondTo[:password, :credit_card]`
|
102
|
+
* [`Send`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Send) – checks that all named methods return a truthy value, e.g. `Send[:valid?]`
|
100
103
|
|
101
104
|
* Miscellaneous
|
102
|
-
* [`Exactly`](http://www.rubydoc.info/gems/contracts/Contracts/Exactly) – checks that the argument has the given type, not accepting sub-classes, e.g. `Exactly[Numeric]`.
|
103
|
-
* [`Eq`](http://www.rubydoc.info/gems/contracts/Contracts/Eq) – checks that the argument is precisely equal to the given value, e.g. `Eq[String]` matches the class `String` and not a string instance.
|
104
|
-
* [`Func`](http://www.rubydoc.info/gems/contracts/Contracts/Func) – specifies the contract for a proc/lambda e.g. `Contract ArrayOf[Num], Func[Num => Num] => ArrayOf[Num]`. See section "Contracts On Functions".
|
105
|
+
* [`Exactly`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Exactly) – checks that the argument has the given type, not accepting sub-classes, e.g. `Exactly[Numeric]`.
|
106
|
+
* [`Eq`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Eq) – checks that the argument is precisely equal to the given value, e.g. `Eq[String]` matches the class `String` and not a string instance.
|
107
|
+
* [`Func`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Func) – specifies the contract for a proc/lambda e.g. `Contract ArrayOf[Num], Func[Num => Num] => ArrayOf[Num]`. See section "Contracts On Functions".
|
105
108
|
|
106
|
-
To see all the built-in contracts and their full descriptions, check out the [RDoc](http://rubydoc.info/gems/contracts/Contracts).
|
109
|
+
To see all the built-in contracts and their full descriptions, check out the [RDoc](http://rubydoc.info/gems/contracts/Contracts/Builtin).
|
107
110
|
|
108
111
|
It is recommended to use shortcut for referring builtin contracts:
|
109
112
|
|
@@ -268,7 +271,7 @@ You don't need to put a contract on every key. So this call would succeed:
|
|
268
271
|
person({:name => "Adit", :age => 42, :foo => "bar"})
|
269
272
|
```
|
270
273
|
|
271
|
-
even though we don't specify a type for `:foo`.
|
274
|
+
even though we don't specify a type for `:foo`. If you need this check though, use `StrictHash` instead.
|
272
275
|
|
273
276
|
Peruse this contract on the keys and values of a Hash.
|
274
277
|
|
@@ -603,6 +606,32 @@ Possible validator overrides:
|
|
603
606
|
|
604
607
|
Default validators can be found here: [lib/contracts/validators.rb](https://github.com/egonSchiele/contracts.ruby/blob/master/lib/contracts/validators.rb).
|
605
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
|
+
|
606
635
|
## Disabling contracts
|
607
636
|
|
608
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/contracts.gemspec
CHANGED
@@ -8,5 +8,10 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.author = "Aditya Bhargava"
|
9
9
|
s.email = "bluemangroupie@gmail.com"
|
10
10
|
s.files = `git ls-files`.split("\n")
|
11
|
-
s.homepage = "
|
11
|
+
s.homepage = "https://github.com/egonSchiele/contracts.ruby"
|
12
|
+
s.license = "BSD-2-Clause"
|
13
|
+
s.post_install_message = "
|
14
|
+
0.16.x will be the supporting Ruby 2.x and be feature frozen (only fixes will be released)
|
15
|
+
For Ruby 3.x use 0.17.x or later (might not be released yet)
|
16
|
+
"
|
12
17
|
end
|
data/dependabot.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
version: 2
|
2
|
+
updates:
|
3
|
+
- package-ecosystem: bundler
|
4
|
+
directory: "/"
|
5
|
+
schedule:
|
6
|
+
interval: monthly
|
7
|
+
time: "06:00"
|
8
|
+
timezone: Asia/Hong_Kong
|
9
|
+
open-pull-requests-limit: 10
|
10
|
+
labels:
|
11
|
+
- "dependencies"
|
12
|
+
- package-ecosystem: github-actions
|
13
|
+
directory: "/"
|
14
|
+
schedule:
|
15
|
+
interval: monthly
|
16
|
+
time: "06:00"
|
17
|
+
timezone: Asia/Hong_Kong
|
18
|
+
open-pull-requests-limit: 10
|
19
|
+
labels:
|
20
|
+
- "dependencies"
|