ree 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +41 -0
- data/LICENSE.txt +21 -0
- data/README.md +474 -0
- data/Rakefile +8 -0
- data/bin/console +8 -0
- data/bin/setup +8 -0
- data/exe/ree +264 -0
- data/lib/ree/args.rb +34 -0
- data/lib/ree/bean_dsl.rb +24 -0
- data/lib/ree/cli/generate_package.rb +18 -0
- data/lib/ree/cli/generate_package_schema.rb +54 -0
- data/lib/ree/cli/generate_packages_schema.rb +17 -0
- data/lib/ree/cli/generate_template.rb +20 -0
- data/lib/ree/cli/init.rb +22 -0
- data/lib/ree/cli/spec_runner.rb +184 -0
- data/lib/ree/cli.rb +12 -0
- data/lib/ree/container.rb +67 -0
- data/lib/ree/contracts/arg_contracts/any.rb +15 -0
- data/lib/ree/contracts/arg_contracts/array_of.rb +52 -0
- data/lib/ree/contracts/arg_contracts/block.rb +15 -0
- data/lib/ree/contracts/arg_contracts/bool.rb +21 -0
- data/lib/ree/contracts/arg_contracts/eq.rb +28 -0
- data/lib/ree/contracts/arg_contracts/exactly.rb +28 -0
- data/lib/ree/contracts/arg_contracts/hash_of.rb +59 -0
- data/lib/ree/contracts/arg_contracts/ksplat.rb +141 -0
- data/lib/ree/contracts/arg_contracts/kwargs.rb +22 -0
- data/lib/ree/contracts/arg_contracts/nilor.rb +16 -0
- data/lib/ree/contracts/arg_contracts/none.rb +15 -0
- data/lib/ree/contracts/arg_contracts/optblock.rb +11 -0
- data/lib/ree/contracts/arg_contracts/or.rb +30 -0
- data/lib/ree/contracts/arg_contracts/range_of.rb +51 -0
- data/lib/ree/contracts/arg_contracts/set_of.rb +54 -0
- data/lib/ree/contracts/arg_contracts/splat.rb +297 -0
- data/lib/ree/contracts/arg_contracts/splat_of.rb +64 -0
- data/lib/ree/contracts/arg_contracts/squarable.rb +11 -0
- data/lib/ree/contracts/arg_contracts/subclass_of.rb +28 -0
- data/lib/ree/contracts/arg_contracts.rb +29 -0
- data/lib/ree/contracts/called_args_validator.rb +291 -0
- data/lib/ree/contracts/contract_definition.rb +142 -0
- data/lib/ree/contracts/contractable.rb +34 -0
- data/lib/ree/contracts/core.rb +17 -0
- data/lib/ree/contracts/engine.rb +71 -0
- data/lib/ree/contracts/engine_proxy.rb +13 -0
- data/lib/ree/contracts/errors/bad_contract_error.rb +4 -0
- data/lib/ree/contracts/errors/contract_error.rb +4 -0
- data/lib/ree/contracts/errors/error.rb +4 -0
- data/lib/ree/contracts/errors/return_contract_error.rb +4 -0
- data/lib/ree/contracts/method_decorator.rb +158 -0
- data/lib/ree/contracts/truncatable.rb +9 -0
- data/lib/ree/contracts/utils.rb +9 -0
- data/lib/ree/contracts/validators/array_validator.rb +51 -0
- data/lib/ree/contracts/validators/base_validator.rb +27 -0
- data/lib/ree/contracts/validators/class_validator.rb +17 -0
- data/lib/ree/contracts/validators/default_validator.rb +20 -0
- data/lib/ree/contracts/validators/hash_validator.rb +100 -0
- data/lib/ree/contracts/validators/proc_validator.rb +17 -0
- data/lib/ree/contracts/validators/range_validator.rb +17 -0
- data/lib/ree/contracts/validators/regexp_validator.rb +17 -0
- data/lib/ree/contracts/validators/valid_validator.rb +28 -0
- data/lib/ree/contracts/validators.rb +42 -0
- data/lib/ree/contracts.rb +45 -0
- data/lib/ree/core/link_validator.rb +42 -0
- data/lib/ree/core/object.rb +132 -0
- data/lib/ree/core/object_error.rb +9 -0
- data/lib/ree/core/object_link.rb +21 -0
- data/lib/ree/core/object_schema.rb +47 -0
- data/lib/ree/core/object_schema_builder.rb +110 -0
- data/lib/ree/core/package.rb +177 -0
- data/lib/ree/core/package_dep.rb +9 -0
- data/lib/ree/core/package_env_var.rb +12 -0
- data/lib/ree/core/package_loader.rb +95 -0
- data/lib/ree/core/package_schema.rb +27 -0
- data/lib/ree/core/package_schema_builder.rb +53 -0
- data/lib/ree/core/package_schema_loader.rb +170 -0
- data/lib/ree/core/packages_detector.rb +43 -0
- data/lib/ree/core/packages_schema.rb +19 -0
- data/lib/ree/core/packages_schema_builder.rb +50 -0
- data/lib/ree/core/packages_schema_loader.rb +95 -0
- data/lib/ree/core/packages_schema_locator.rb +27 -0
- data/lib/ree/core/packages_store.rb +32 -0
- data/lib/ree/core/path_helper.rb +104 -0
- data/lib/ree/dsl/build_package_dsl.rb +155 -0
- data/lib/ree/dsl/domain_error.rb +4 -0
- data/lib/ree/dsl/error_builder.rb +39 -0
- data/lib/ree/dsl/error_dsl.rb +27 -0
- data/lib/ree/dsl/import_dsl.rb +106 -0
- data/lib/ree/dsl/link_import_builder.rb +66 -0
- data/lib/ree/dsl/object_dsl.rb +319 -0
- data/lib/ree/dsl/object_hooks.rb +6 -0
- data/lib/ree/dsl/package_require.rb +44 -0
- data/lib/ree/error.rb +11 -0
- data/lib/ree/facades/packages_facade.rb +197 -0
- data/lib/ree/fn_dsl.rb +24 -0
- data/lib/ree/gen/init.rb +64 -0
- data/lib/ree/gen/package.rb +56 -0
- data/lib/ree/gen.rb +8 -0
- data/lib/ree/handlers/template_handler.rb +118 -0
- data/lib/ree/link_dsl.rb +175 -0
- data/lib/ree/object_compiler.rb +149 -0
- data/lib/ree/package_dsl.rb +34 -0
- data/lib/ree/rspec_link_dsl.rb +19 -0
- data/lib/ree/spec_runner/command_generator.rb +49 -0
- data/lib/ree/spec_runner/command_params.rb +9 -0
- data/lib/ree/spec_runner/runner.rb +200 -0
- data/lib/ree/spec_runner/spec_filename_matcher.rb +27 -0
- data/lib/ree/spec_runner/view.rb +30 -0
- data/lib/ree/spec_runner.rb +11 -0
- data/lib/ree/templates/init/.gitignore +1 -0
- data/lib/ree/templates/init/.irbrc +13 -0
- data/lib/ree/templates/init/.rspec +3 -0
- data/lib/ree/templates/init/.ruby-version +1 -0
- data/lib/ree/templates/init/Gemfile +7 -0
- data/lib/ree/templates/init/Packages.schema.json +1 -0
- data/lib/ree/templates/init/bin/console +5 -0
- data/lib/ree/templates/init/readme.md +2 -0
- data/lib/ree/templates/init/ree.setup.rb +21 -0
- data/lib/ree/templates/init/spec.init.rb +7 -0
- data/lib/ree/templates/package/.gitignore +0 -0
- data/lib/ree/templates/package/.rspec +2 -0
- data/lib/ree/templates/package/<%=package_subdir_name%>/<%=package_name%>/.gitkeep +0 -0
- data/lib/ree/templates/package/<%=package_subdir_name%>/<%=package_name%>.rb +15 -0
- data/lib/ree/templates/package/Package.schema.json +0 -0
- data/lib/ree/templates/package/bin/console +5 -0
- data/lib/ree/templates/package/spec/package_schema_spec.rb +14 -0
- data/lib/ree/templates/package/spec/spec_helper.rb +3 -0
- data/lib/ree/templates/template_detector.rb +35 -0
- data/lib/ree/templates/template_renderer.rb +55 -0
- data/lib/ree/utils/render_utils.rb +20 -0
- data/lib/ree/utils/string_utils.rb +29 -0
- data/lib/ree/version.rb +5 -0
- data/lib/ree.rb +279 -0
- data/sig/ree.rbs +4 -0
- metadata +199 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3b3d64c6833699e7eab9bb131f57e8f6bc6b3d68e609dcdc261b78029a22d070
|
4
|
+
data.tar.gz: 9092fc80679dd84185e74de7639771b6bb625f904d0d4883b00f46b705c448f4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 18e0f5cf506e899bc269ff31af9941b2e6fe9e47d0d3a36bb1acf7db5335326efb6511f1125434870354a425330f98c5d07f10081b2cd97d8416a7650a0661c5
|
7
|
+
data.tar.gz: 793ad63ee56c2af4ba302516ae4d1a82da280433a1eb5ac78abcb9217bb74d90f4ecba015066da845ad0828c4a0426bec67c5c1ea2286f80846f6c07fa8cd352
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.1.2
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ruslan.gatiyatov@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ree (1.0.0)
|
5
|
+
commander (~> 4.6.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
commander (4.6.0)
|
11
|
+
highline (~> 2.0.0)
|
12
|
+
diff-lcs (1.5.0)
|
13
|
+
highline (2.0.3)
|
14
|
+
rake (13.0.6)
|
15
|
+
rspec (3.11.0)
|
16
|
+
rspec-core (~> 3.11.0)
|
17
|
+
rspec-expectations (~> 3.11.0)
|
18
|
+
rspec-mocks (~> 3.11.0)
|
19
|
+
rspec-core (3.11.0)
|
20
|
+
rspec-support (~> 3.11.0)
|
21
|
+
rspec-expectations (3.11.0)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.11.0)
|
24
|
+
rspec-mocks (3.11.1)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.11.0)
|
27
|
+
rspec-support (3.11.0)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
arm64-darwin-21
|
31
|
+
x86_64-darwin-19
|
32
|
+
x86_64-darwin-21
|
33
|
+
x86_64-linux
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
rake (~> 13.0)
|
37
|
+
ree!
|
38
|
+
rspec (~> 3.0)
|
39
|
+
|
40
|
+
BUNDLED WITH
|
41
|
+
2.3.7
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Ruslan Gatiyatov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,474 @@
|
|
1
|
+
# Introduction to Ree
|
2
|
+
|
3
|
+
Ree is a ruby framework to create modular Ruby applications.
|
4
|
+
Ree introduces list of a following tools and concepts:
|
5
|
+
|
6
|
+
- Commands
|
7
|
+
- Project
|
8
|
+
- Packages
|
9
|
+
- Objects
|
10
|
+
- Contracts
|
11
|
+
- Schemas (Packages, Package, Object)
|
12
|
+
- Core Ruby Library Extensions
|
13
|
+
|
14
|
+
Before you start working with Ree framework please read documentation below to get usage examples and full understanding of the concepts provided.
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
```ruby
|
19
|
+
gem 'ree'
|
20
|
+
```
|
21
|
+
And then execute:
|
22
|
+
```bash
|
23
|
+
$ bundle install
|
24
|
+
```
|
25
|
+
Or install a gem into the local repository:
|
26
|
+
```bash
|
27
|
+
$ gem install ree
|
28
|
+
```
|
29
|
+
|
30
|
+
## Ree Commands
|
31
|
+
|
32
|
+
After Ree installation one would get `$ ree` binary available in your shell.
|
33
|
+
|
34
|
+
`$ ree help` provides list of all available commands
|
35
|
+
`$ ree help COMMAND_NAME` shows command description with usage examples
|
36
|
+
`$ ree init [options]` generates project scaffold inside current directory
|
37
|
+
`$ ree exec PACKAGE_NAME PATH_TO_EXEC [options]` executes binary file from specific package folder
|
38
|
+
`$ ree spec PACKAGE_NAME SPEC_MATCHER [options]` runs rspec tests for specific package
|
39
|
+
`$ ree gen.package PACKAGE_NAME [options]` generates a package scaffold in a specified folder
|
40
|
+
`$ ree gen.packages_json [options]` generates Packages.schema.json
|
41
|
+
`$ ree gen.package_json PACKAGE_NAME [options]` generates Package.schema.json for specific package
|
42
|
+
`$ ree gen.template TEMPLATE_NAME [options]` generates template from PROJECT_DIR/.ree/templates/TEMPLATE_NAME folder with specified variables
|
43
|
+
|
44
|
+
## Setup Ree Project
|
45
|
+
First of all `cd` into your project root folder.
|
46
|
+
Run `ree init` command to generate Ree project scaffold. One would get output like this:
|
47
|
+
```bash
|
48
|
+
Generated: .rspec
|
49
|
+
Generated: .ruby-version
|
50
|
+
Generated: Gemfile
|
51
|
+
Generated: Packages.schema.json
|
52
|
+
Generated: bin/console
|
53
|
+
Generated: readme.md
|
54
|
+
Generated: ree.setup.rb
|
55
|
+
Generated: spec.init.rb
|
56
|
+
```
|
57
|
+
|
58
|
+
`.rspec` file should be used to define global Rspec options.
|
59
|
+
`.ruby-version` By default this file would be populated with ruby version used to run `ree init` command.
|
60
|
+
`Gemfile` Global project Gemfile. Read about global Gemfile concept below.
|
61
|
+
`Packages.schema.json` Autogenerated schema of all project packages. Read information about Ree schemas below.
|
62
|
+
`bin/console` Global project console
|
63
|
+
`readme.md` Documentation for your project
|
64
|
+
`ree.setup.rb` This is a project setup file. It's being loaded when project starts.
|
65
|
+
`spec.init.rb` Global Rspec configuration file. One should use this file to load something before rspec suite starts.
|
66
|
+
|
67
|
+
## Global Gemfile Concept
|
68
|
+
Ree uses Global Gemfile Concept - one Gemfile for entire project. Ree-gems has there own gem dependencies (see gem packaging section). As a best practice we recommend to add `require: false` option to all none-ree gems in your `Gemfile`.
|
69
|
+
```ruby
|
70
|
+
opts = {require: false}
|
71
|
+
|
72
|
+
gem 'ree'
|
73
|
+
gem 'oj', opts
|
74
|
+
gem 'roda', opts
|
75
|
+
```
|
76
|
+
Gems should be required in the package entry files (read about packages below).
|
77
|
+
```ruby
|
78
|
+
# package entry path: ./packages/ree_json/package/ree_json.rb
|
79
|
+
require 'oj'
|
80
|
+
|
81
|
+
module ReeJson
|
82
|
+
include Ree::PackageDSL
|
83
|
+
|
84
|
+
package
|
85
|
+
end
|
86
|
+
```
|
87
|
+
This practice gives you the following benefits:
|
88
|
+
- single place to manage all your project gems
|
89
|
+
- easy way to understand which packages introduced gems to the project
|
90
|
+
- faster boot time for Rspec suite & App
|
91
|
+
|
92
|
+
## Ree Packages
|
93
|
+
Coming soon...
|
94
|
+
|
95
|
+
## Ree Objects
|
96
|
+
Coming soon...
|
97
|
+
|
98
|
+
## Ree Contracts
|
99
|
+
Ree contracts bring code contracts to the Ruby language. Code contracts allow you to make some assertions about your code, and then check them to make sure they hold. This lets you:
|
100
|
+
- speed up your development and catch bugs earlier
|
101
|
+
- get full picture of method (arguments and there restrictions, method return value, thrown exceptions)
|
102
|
+
- make sure that the user gets proper messaging when a bug occurs.
|
103
|
+
|
104
|
+
### Basics
|
105
|
+
A simple example:
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
contract Integer, Integer => Integer
|
109
|
+
def add(a, b)
|
110
|
+
a + b
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
114
|
+
Here, the contract is `contract Integer, Integer => Integer`. This says that the add function takes two integers and returns an integer.
|
115
|
+
|
116
|
+
Copy this code into a file and run it:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
require 'ree'
|
120
|
+
|
121
|
+
class Calc
|
122
|
+
contract Integer, Integer => Integer
|
123
|
+
def self.add(a, b)
|
124
|
+
a + b
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
Calc.add(1, "foo")
|
129
|
+
```
|
130
|
+
You'll see a detailed error message like so:
|
131
|
+
```
|
132
|
+
Contract violation for Calc.add
|
133
|
+
- b: expected Integer, got String => "foo"
|
134
|
+
```
|
135
|
+
|
136
|
+
That tells you that your contract was violated! Method Call.add expected an Integer, and got a String ("foo") instead.
|
137
|
+
|
138
|
+
### Built-in Contracts
|
139
|
+
|
140
|
+
Ree contracts comes with a lot of built-in contracts, including the following:
|
141
|
+
- Basic types:
|
142
|
+
- **Bool** – checks that the argument is true or false
|
143
|
+
- **Any** – Passes for any argument. Use when the argument has no constraints.
|
144
|
+
- **None** – Fails for any argument. Use when the method takes no arguments.
|
145
|
+
- Logical combinations:
|
146
|
+
- **Nilor** – specifies that a value may be nil, e.g. `Nilor[String]` (equivalent to `Or[String, nil]`)
|
147
|
+
- **Or** – passes if any of the given contracts pass, e.g. `Or[Fixnum, Float]`
|
148
|
+
- Collections
|
149
|
+
- **ArrayOf** – checks that the argument is an array, and all elements pass the given contract, e.g. `ArrayOf[Integer]`
|
150
|
+
- **SetOf** – checks that the argument is a set, and all elements pass the given contract, e.g. `SetOf[Integer]`
|
151
|
+
- **HashOf** – checks that the argument is a hash, and all keys and values pass the given contract, e.g. `HashOf[Symbol, String]`
|
152
|
+
- **RangeOf** – checks that the argument is a range whose elements (#first and #last) pass the given contract, e.g. `RangeOf[Date]`
|
153
|
+
- Array contracts
|
154
|
+
- `[String, Symbol]` defines a contract for argument accepting `Array` of 2 items having symbol and string => `['hello', :world]`
|
155
|
+
- Hash contracts
|
156
|
+
- `{a: Integer, b?: String}` defines a contract for argument accepting `Hash` with required `a` key having `Integer` value and optional `b` key having `String` value if present => `{a: 1, b: 'string'}` or `{a: 1}`
|
157
|
+
- Range contracts
|
158
|
+
- `(1..20)` defines a contract for argument accepting argument value in the specified range.
|
159
|
+
- Splat contracts
|
160
|
+
- **SplatOf** - checks that all elements of splat argument pass the given contract `SplatOf[String]`
|
161
|
+
- **Splat** - allows to combine other contracts with `SplatOf` contract, e.g. `Splat[SplatOf[String], Symbol]` or `Splat[Symbol, SplatOf[String]]` or `Splat[Integer, SplatOf[String], Symbol]`
|
162
|
+
- Contracts for keyword arguments
|
163
|
+
- **Kwargs** – checks that the argument is an options hash, and all required keyword arguments are present, and all values pass their respective contracts, e.g. `Kwargs[number: Integer, description: Optional[String]]`
|
164
|
+
- Contracts for keyword splat arguments
|
165
|
+
- **Ksplat** – checks that the argument is an options hash, and all required or optional keyword splat arguments are present, and all values pass their respective contracts, e.g. `Ksplat[number: Integer, description?: Optional[String]]`. Here number is a required keyword splat argument, description is an optional keyword splat argument.
|
166
|
+
- Duck typing
|
167
|
+
- **RespondTo** – checks that the argument responds to all of the given methods, e.g. `RespondTo[:password, :credit_card]`
|
168
|
+
- Miscellaneous
|
169
|
+
- **Block** – checks that method has required block argument.
|
170
|
+
- **Optblock** – checks that method has optional block argument.
|
171
|
+
- **Exactly** – checks that the argument has the given type, not accepting sub-classes, e.g. `Exactly[Integer]`.
|
172
|
+
- **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.
|
173
|
+
- **SubclassOf** – checks that the argument is a Class and is a subclass of specific class, e.g. `SubclassOf[Integer]`.
|
174
|
+
|
175
|
+
### More Examples
|
176
|
+
####Hello, World
|
177
|
+
```ruby
|
178
|
+
contract String => nil
|
179
|
+
def hello(name)
|
180
|
+
puts "hello, #{name}!"
|
181
|
+
end
|
182
|
+
```
|
183
|
+
You always need to specify a contract for the return value. In this example, `hello` doesn't return anything, so the contract is nil. Now you know that you can use a constant like nil as the end of a contract. Valid values for a contract are:
|
184
|
+
|
185
|
+
- the name of a class (like String or Fixnum)
|
186
|
+
- a constant (like nil or 1)
|
187
|
+
- one of built-in contracts
|
188
|
+
- a Proc that takes a value and returns true or false to indicate whether the contract passed or not
|
189
|
+
- a class or an object that responds to the valid? (more on this later)
|
190
|
+
|
191
|
+
Some functions do not have arguments. Here is how to define contracts for them.
|
192
|
+
```ruby
|
193
|
+
contract None => nil
|
194
|
+
def hello
|
195
|
+
puts 'hello'
|
196
|
+
end
|
197
|
+
```
|
198
|
+
|
199
|
+
#### Function with required block
|
200
|
+
Sometimes you want to force user to provide block argument:
|
201
|
+
```ruby
|
202
|
+
contract Block => nil
|
203
|
+
def hello(&blk)
|
204
|
+
puts 'hello'
|
205
|
+
end
|
206
|
+
|
207
|
+
# success
|
208
|
+
hello { puts 'world' }
|
209
|
+
|
210
|
+
#failure
|
211
|
+
hello
|
212
|
+
```
|
213
|
+
|
214
|
+
#### Function with optional block
|
215
|
+
Optional block arguments should be defined like this:
|
216
|
+
```ruby
|
217
|
+
contract Optblock => nil
|
218
|
+
def hello(&blk)
|
219
|
+
puts 'hello'
|
220
|
+
end
|
221
|
+
|
222
|
+
# both call will be successfull
|
223
|
+
hello { puts 'world' }
|
224
|
+
hello
|
225
|
+
```
|
226
|
+
|
227
|
+
####A Double Function
|
228
|
+
```ruby
|
229
|
+
contract Or[Fixnum, Float] => Or[Fixnum, Float]
|
230
|
+
def double(x)
|
231
|
+
2 * x
|
232
|
+
end
|
233
|
+
```
|
234
|
+
Sometimes you want to be able to choose between a few contracts. `Or` takes a variable number of contracts and checks the argument against all of them. If it passes for any of the contracts, then the Or contract passes. This introduces some new syntax. One of the valid values for a contract is an instance of a class that responds to the valid? method. This is what `Or[Fixnum, Float]` is. The longer way to write it would have been:
|
235
|
+
```ruby
|
236
|
+
contract Or.new(Fixnum, Float) => Or.new(Fixnum, Float)
|
237
|
+
```
|
238
|
+
All the built-in contracts have overridden the square brackets `[]` to give the same functionality. So you could write
|
239
|
+
```ruby
|
240
|
+
contract Or[Fixnum, Float] => Or[Fixnum, Float]
|
241
|
+
```
|
242
|
+
or
|
243
|
+
```ruby
|
244
|
+
contract Or.new(Fixnum, Float) => Or.new(Fixnum, Float)
|
245
|
+
```
|
246
|
+
whichever you prefer. They both mean the same thing here: make a new instance of `Or` with `Fixnum` and `Float`. Use that instance to validate the argument.
|
247
|
+
|
248
|
+
#### A Product Function
|
249
|
+
```ruby
|
250
|
+
contract ArrayOf[Integer] => Integer
|
251
|
+
def product(vals)
|
252
|
+
total = 1
|
253
|
+
|
254
|
+
vals.each do |val|
|
255
|
+
total *= val
|
256
|
+
end
|
257
|
+
|
258
|
+
total
|
259
|
+
end
|
260
|
+
```
|
261
|
+
This contract uses the `ArrayOf` contract. Here's how `ArrayOf` works: it takes a contract. It expects the argument to be a list. Then it checks every value in that list to see if it satisfies that contract.
|
262
|
+
```ruby
|
263
|
+
# passes
|
264
|
+
product([1, 2, 3, 4])
|
265
|
+
|
266
|
+
# fails
|
267
|
+
product([1, 2, 3, "foo"])
|
268
|
+
```
|
269
|
+
|
270
|
+
#### Another Product Function
|
271
|
+
```ruby
|
272
|
+
contract SplatOf[Integer] => Integer
|
273
|
+
def product(*vals)
|
274
|
+
total = 1
|
275
|
+
|
276
|
+
vals.each do |val|
|
277
|
+
total *= val
|
278
|
+
end
|
279
|
+
|
280
|
+
total
|
281
|
+
end
|
282
|
+
|
283
|
+
product(1, 2, 3)
|
284
|
+
```
|
285
|
+
This function uses splat args `*vals` instead of an array. To make a contract on splat args, use the SplatOf contract. It takes one contract as an argument and uses it to validate every element passed in through *vals. So for example,
|
286
|
+
|
287
|
+
`SplatOf[Integer]` means they should all be numbers.
|
288
|
+
|
289
|
+
`SplatOf[Or[Integer, String]]` means they should all be numbers or strings.
|
290
|
+
|
291
|
+
`SplatOf[Any]` means all arguments are allowed (Any is a contract that passes for any argument).
|
292
|
+
|
293
|
+
|
294
|
+
#### Contracts On Arrays
|
295
|
+
If an array is one of the arguments and you know how many elements it's going to have, you can put a contract on it:
|
296
|
+
```ruby
|
297
|
+
# a function that takes an array of two elements...a person's age and a person's name.
|
298
|
+
contract [Integer, String] => nil
|
299
|
+
def person(data)
|
300
|
+
p data
|
301
|
+
end
|
302
|
+
```
|
303
|
+
If you don't know how many elements it's going to have, use `ArrayOf`.
|
304
|
+
```ruby
|
305
|
+
contract ArrayOf[String] => nil
|
306
|
+
def person(data)
|
307
|
+
p data
|
308
|
+
end
|
309
|
+
```
|
310
|
+
#### Contracts On Hashes
|
311
|
+
Here's a contract that requires a Hash. We can put contracts on each of the keys:
|
312
|
+
```ruby
|
313
|
+
# note that you can define optional Hash arguments using ? mark in the end of key name.
|
314
|
+
contract {age: Integer, name: String, sirname?: String } => nil
|
315
|
+
def person(data)
|
316
|
+
p data
|
317
|
+
end
|
318
|
+
|
319
|
+
# succeeds
|
320
|
+
person(age: 21, name: 'John')
|
321
|
+
person(age: 21, name: 'John', sirname: 'Doe')
|
322
|
+
# fails
|
323
|
+
person(other: 21)
|
324
|
+
```
|
325
|
+
|
326
|
+
Peruse this contract on the keys and values of a Hash.
|
327
|
+
```ruby
|
328
|
+
contract HashOf[Symbol, Integer] => Integer
|
329
|
+
def give_largest_value(hsh)
|
330
|
+
hsh.values.max
|
331
|
+
end
|
332
|
+
```
|
333
|
+
Which you use like so:
|
334
|
+
```ruby
|
335
|
+
# succeeds
|
336
|
+
give_largest_value(a: 1, b: 2, c: 3) # returns 3
|
337
|
+
|
338
|
+
# fails
|
339
|
+
give_largest_value("a" => 1, 2 => 2, c: 3)
|
340
|
+
```
|
341
|
+
|
342
|
+
#### Contracts On Strings
|
343
|
+
When you want a contract to match not just any string (i.e. `contract String => nil`), you can use regular expressions:
|
344
|
+
```ruby
|
345
|
+
contract /World|Mars/i => nil
|
346
|
+
def greet(name)
|
347
|
+
puts "Hello #{name}!"
|
348
|
+
end
|
349
|
+
```
|
350
|
+
|
351
|
+
#### Contracts On Keyword Arguments
|
352
|
+
Lets say you are writing a simple function and require a bunch of keyword arguments:
|
353
|
+
```ruby
|
354
|
+
contract String, Kwargs[port: Integer, user: String, password: String] => Connection
|
355
|
+
def connect(host, port: 5000, user:, password:)
|
356
|
+
# ...
|
357
|
+
end
|
358
|
+
|
359
|
+
# No value is passed for port
|
360
|
+
connect("example.org", user: "me", password: "none")
|
361
|
+
```
|
362
|
+
In case function does not have any optional keyword args one can avoid usage of `Kwargs` at all:
|
363
|
+
```ruby
|
364
|
+
contract String, Integer, String, String => Connection
|
365
|
+
def connect(host, port:, user:, password:)
|
366
|
+
# ...
|
367
|
+
end
|
368
|
+
|
369
|
+
# No value is passed for port
|
370
|
+
connect("example.org", user: "me", password: "none")
|
371
|
+
```
|
372
|
+
|
373
|
+
#### Contracts On Keyword Splat Arguments
|
374
|
+
In most of the cases keyword splat arguments should be used to define optional keyword arguments:
|
375
|
+
|
376
|
+
```ruby
|
377
|
+
contract String, Ksplat[sirname?: String] => nil
|
378
|
+
def hello(name, **opts)
|
379
|
+
str = name
|
380
|
+
|
381
|
+
if opts[:String]
|
382
|
+
str += " #{opts[:sirname]}"
|
383
|
+
end
|
384
|
+
|
385
|
+
puts str
|
386
|
+
end
|
387
|
+
```
|
388
|
+
|
389
|
+
#### Returning Multiple Values
|
390
|
+
Treat the return value as an array. For example, here's a function that returns two numbers:
|
391
|
+
|
392
|
+
```ruby
|
393
|
+
contract Integer => [Integer, Integer]
|
394
|
+
def mult(x)
|
395
|
+
return x, x+1
|
396
|
+
end
|
397
|
+
```
|
398
|
+
|
399
|
+
### Defining Your Own Contracts
|
400
|
+
|
401
|
+
Contracts are very easy to define. To re-iterate, there are 5 kinds of contracts:
|
402
|
+
|
403
|
+
- the name of a class (like String or Fixnum)
|
404
|
+
- a constant (like nil or 1)
|
405
|
+
- a Proc that takes a value and returns true or false to indicate whether the contract passed or not
|
406
|
+
- a class that responds to the valid? class method (more on this later)
|
407
|
+
- an instance of a class that responds to the valid? method (more on this later)
|
408
|
+
|
409
|
+
The first two don't need any extra work to define: you can just use any constant or class name in your contract and it should just work. Here are examples for the rest:
|
410
|
+
|
411
|
+
|
412
|
+
#### A Proc
|
413
|
+
```ruby
|
414
|
+
contract Proc.new { |x| x.is_a? Numeric } => Integer
|
415
|
+
def double(x)
|
416
|
+
```
|
417
|
+
The `Proc` takes one parameter: the argument that is getting passed to the function. It checks to see if it's a Numeric. If it is, it returns true. Otherwise it returns false. It's not good practice to write a Proc right in your contract...if you find yourself doing it often, write it as a class instead.
|
418
|
+
|
419
|
+
#### A Class With valid? As a Class Method
|
420
|
+
```ruby
|
421
|
+
class Num
|
422
|
+
def self.valid? val
|
423
|
+
val.is_a? Numeric
|
424
|
+
end
|
425
|
+
end
|
426
|
+
```
|
427
|
+
The `valid?` class method takes one parameter: the argument that is getting passed to the function. It returns true or false.
|
428
|
+
```ruby
|
429
|
+
contract Num => Num
|
430
|
+
def double(x)
|
431
|
+
x
|
432
|
+
end
|
433
|
+
```
|
434
|
+
|
435
|
+
#### A Class With valid? As an Instance Method
|
436
|
+
```ruby
|
437
|
+
class Num
|
438
|
+
def valid? val
|
439
|
+
val.is_a? Numeric
|
440
|
+
end
|
441
|
+
end
|
442
|
+
|
443
|
+
contract Num.new => Num.new
|
444
|
+
def double(x)
|
445
|
+
x
|
446
|
+
end
|
447
|
+
```
|
448
|
+
|
449
|
+
### Disabling contracts
|
450
|
+
If you want to disable contracts, set the `NO_CONTRACTS` environment variable. This will disable contracts and you won't have a performance hit.
|
451
|
+
|
452
|
+
## Ree Schemas
|
453
|
+
Coming soon...
|
454
|
+
|
455
|
+
## Ruby Std Lib Extensions
|
456
|
+
Coming soon...
|
457
|
+
|
458
|
+
## Development
|
459
|
+
|
460
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
461
|
+
|
462
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
463
|
+
|
464
|
+
## Contributing
|
465
|
+
|
466
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/glabix/ree. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/glabix/ree/ree/blob/main/CODE_OF_CONDUCT.md).
|
467
|
+
|
468
|
+
## License
|
469
|
+
|
470
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
471
|
+
|
472
|
+
## Code of Conduct
|
473
|
+
|
474
|
+
Everyone interacting in the Ree project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/glabix/ree/ree/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED