bcdd-process 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +90 -0
- data/.rubocop_todo.yml +28 -0
- data/CHANGELOG.md +11 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +110 -0
- data/Rakefile +18 -0
- data/examples/business_processes/Rakefile +56 -0
- data/examples/business_processes/app/models/account/member.rb +10 -0
- data/examples/business_processes/app/models/account/owner_creation.rb +59 -0
- data/examples/business_processes/app/models/account.rb +11 -0
- data/examples/business_processes/app/models/user/creation.rb +61 -0
- data/examples/business_processes/app/models/user/token/creation.rb +47 -0
- data/examples/business_processes/app/models/user/token.rb +7 -0
- data/examples/business_processes/app/models/user.rb +15 -0
- data/examples/business_processes/config/boot.rb +16 -0
- data/examples/business_processes/config/initializers/bcdd.rb +11 -0
- data/examples/business_processes/config.rb +28 -0
- data/examples/business_processes/db/setup.rb +61 -0
- data/examples/business_processes/lib/bcdd/contracts.rb +30 -0
- data/examples/business_processes/lib/bcdd/result/rollback_on_failure.rb +15 -0
- data/examples/business_processes/lib/bcdd/result/transitions_record.rb +34 -0
- data/examples/business_processes/lib/runtime_breaker.rb +11 -0
- data/examples/business_processes/lib/transitions_listener/stdout.rb +54 -0
- data/lib/bcdd/process/caller.rb +49 -0
- data/lib/bcdd/process/input_spec.rb +72 -0
- data/lib/bcdd/process/output_spec.rb +31 -0
- data/lib/bcdd/process/version.rb +7 -0
- data/lib/bcdd/process.rb +61 -0
- data/lib/bcdd-process.rb +3 -0
- data/sig/bcdd/process.rbs +6 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9aec3a08f8e5f7dd092929d24fb76c2c1b95d07bf13b3683256daafe19595070
|
4
|
+
data.tar.gz: bb29f54a9ec8129bfbb52a1465736df59fa0c34957dc5cebe9566c3d312ec040
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 15b804dd0b7b72c1e2c93013619677f1ca45db9f9fe56cb818c83ef59c2974064304366475d43b6e0a2232c677bff7edc76c439c773c083868e3fe1600ef06d9
|
7
|
+
data.tar.gz: 02da1b070eb0f1ad40b5c04e719aae2759aaaaca9fac878c4abc4afb588a788032417d9a3a4f754baeb81a70d9dbc9f2d41ac485917a24f547442a4b6e855768
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
require:
|
4
|
+
- rubocop-performance
|
5
|
+
- rubocop-minitest
|
6
|
+
- rubocop-rake
|
7
|
+
|
8
|
+
AllCops:
|
9
|
+
NewCops: enable
|
10
|
+
TargetRubyVersion: 2.7
|
11
|
+
Exclude:
|
12
|
+
- 'examples/**/*'
|
13
|
+
- 'vendor/**/*'
|
14
|
+
- 'spec/fixtures/**/*'
|
15
|
+
- 'tmp/**/*'
|
16
|
+
- '.git/**/*'
|
17
|
+
- 'bin/*'
|
18
|
+
|
19
|
+
Layout/LineLength:
|
20
|
+
Max: 120
|
21
|
+
|
22
|
+
Layout/ExtraSpacing:
|
23
|
+
AllowBeforeTrailingComments: true
|
24
|
+
|
25
|
+
Layout/MultilineMethodCallIndentation:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
Lint/UnderscorePrefixedVariableName:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Style/Documentation:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
Style/AccessModifierDeclarations:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Style/CaseEquality:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
Style/ClassAndModuleChildren:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
Style/Lambda:
|
44
|
+
EnforcedStyle: literal
|
45
|
+
|
46
|
+
Style/ParallelAssignment:
|
47
|
+
Enabled: false
|
48
|
+
|
49
|
+
Style/SingleLineMethods:
|
50
|
+
Enabled: false
|
51
|
+
|
52
|
+
Style/MapToSet:
|
53
|
+
Enabled: false
|
54
|
+
|
55
|
+
Style/MixinGrouping:
|
56
|
+
Enabled: false
|
57
|
+
|
58
|
+
Naming/MethodName:
|
59
|
+
Enabled: false
|
60
|
+
|
61
|
+
Naming/VariableName:
|
62
|
+
Enabled: false
|
63
|
+
|
64
|
+
Naming/MemoizedInstanceVariableName:
|
65
|
+
Enabled: false
|
66
|
+
|
67
|
+
Metrics/BlockLength:
|
68
|
+
Exclude:
|
69
|
+
- test/**/*.rb
|
70
|
+
|
71
|
+
Metrics/ClassLength:
|
72
|
+
Exclude:
|
73
|
+
- test/**/*.rb
|
74
|
+
|
75
|
+
Minitest/MultipleAssertions:
|
76
|
+
Enabled: false
|
77
|
+
|
78
|
+
Minitest/AssertEmptyLiteral:
|
79
|
+
Enabled: false
|
80
|
+
|
81
|
+
Minitest/AssertOperator:
|
82
|
+
Enabled: false
|
83
|
+
|
84
|
+
Minitest/AssertWithExpectedArgument:
|
85
|
+
Exclude:
|
86
|
+
- test/test_helper.rb
|
87
|
+
|
88
|
+
Naming/FileName:
|
89
|
+
Exclude:
|
90
|
+
- lib/bcdd-process.rb
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2024-02-01 01:06:41 UTC using RuboCop version 1.60.2.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 1
|
10
|
+
# Configuration parameters: AllowedParentClasses.
|
11
|
+
Lint/MissingSuper:
|
12
|
+
Exclude:
|
13
|
+
- 'lib/bcdd/process.rb'
|
14
|
+
|
15
|
+
# Offense count: 1
|
16
|
+
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
|
17
|
+
Metrics/AbcSize:
|
18
|
+
Max: 20
|
19
|
+
|
20
|
+
# Offense count: 1
|
21
|
+
# Configuration parameters: AllowedMethods, AllowedPatterns.
|
22
|
+
Metrics/CyclomaticComplexity:
|
23
|
+
Max: 8
|
24
|
+
|
25
|
+
# Offense count: 1
|
26
|
+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
|
27
|
+
Metrics/MethodLength:
|
28
|
+
Max: 13
|
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 rodrigo.serradura@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/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Rodrigo Serradura
|
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,110 @@
|
|
1
|
+
<p align="center">
|
2
|
+
<h1 align="center" id="-bcddresult">🚄 BCDD::Process</h1>
|
3
|
+
<p align="center"><i>Write reliable, self-documented, and self-observable business processes in Ruby.</i></p>
|
4
|
+
</p>
|
5
|
+
|
6
|
+
## Ruby Version
|
7
|
+
|
8
|
+
`>= 2.7.0`
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'bcdd-process'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle install
|
21
|
+
|
22
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
23
|
+
|
24
|
+
$ gem install bcdd-process
|
25
|
+
|
26
|
+
And require it in your code:
|
27
|
+
|
28
|
+
require 'bcdd/process'
|
29
|
+
|
30
|
+
## Usage Example
|
31
|
+
|
32
|
+
Check out the [examples/business_processes](examples/business_processes) directory for a complete (Rails like app) example.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class User
|
36
|
+
class Creation < ::BCDD::Process
|
37
|
+
include BCDD::Result::RollbackOnFailure
|
38
|
+
|
39
|
+
input do
|
40
|
+
attribute :uuid, contract: :is_uuid, default: -> { ::SecureRandom.uuid }, normalize: -> { _1.strip.downcase }
|
41
|
+
attribute :name, contract: :is_str, normalize: -> { _1.strip.gsub(/\s+/, ' ') }
|
42
|
+
attribute :email, contract: :is_email, normalize: -> { _1.strip.downcase }
|
43
|
+
attribute :password, contract: :is_password
|
44
|
+
attribute :password_confirmation, contract: :is_password
|
45
|
+
end
|
46
|
+
|
47
|
+
output do
|
48
|
+
Failure(
|
49
|
+
invalid_user: :errors_by_attribute,
|
50
|
+
email_already_taken: :empty_hash,
|
51
|
+
)
|
52
|
+
|
53
|
+
user = contract[::User] & :is_persisted
|
54
|
+
token = contract[Token] & :is_persisted
|
55
|
+
|
56
|
+
Success(user_created: { user:, token: })
|
57
|
+
end
|
58
|
+
|
59
|
+
def call(**input)
|
60
|
+
Given(input)
|
61
|
+
.and_then(:validate_email_uniqueness)
|
62
|
+
.then { |result|
|
63
|
+
rollback_on_failure {
|
64
|
+
result
|
65
|
+
.and_then(:create_user)
|
66
|
+
.and_then(:create_user_token)
|
67
|
+
}
|
68
|
+
}
|
69
|
+
.and_expose(:user_created, %i[user token])
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def validate_email_uniqueness(email:, **)
|
75
|
+
::User.exists?(email:) ? Failure(:email_already_taken) : Continue()
|
76
|
+
end
|
77
|
+
|
78
|
+
def create_user(uuid:, name:, email:, password:, password_confirmation:)
|
79
|
+
user = ::User.create(uuid:, name:, email:, password:, password_confirmation:)
|
80
|
+
|
81
|
+
user.persisted? ? Continue(user:) : Failure(:invalid_user, **user.errors.messages)
|
82
|
+
end
|
83
|
+
|
84
|
+
def create_user_token(user:, **)
|
85
|
+
Token::Creation.new.call(user: user).handle do |on|
|
86
|
+
on.success { |output| Continue(token: output[:token]) }
|
87
|
+
on.failure { raise 'Token creation failed' }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
## Development
|
95
|
+
|
96
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
97
|
+
|
98
|
+
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).
|
99
|
+
|
100
|
+
## Contributing
|
101
|
+
|
102
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/b-cdd/process. 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/b-cdd/process/blob/master/CODE_OF_CONDUCT.md).
|
103
|
+
|
104
|
+
## License
|
105
|
+
|
106
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
107
|
+
|
108
|
+
## Code of Conduct
|
109
|
+
|
110
|
+
Everyone interacting in the BCDD::Process project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/b-cdd/process/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.warning = false
|
8
|
+
|
9
|
+
t.libs += %w[lib test]
|
10
|
+
|
11
|
+
t.test_files = FileList.new('test/**/*_test.rb')
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'rubocop/rake_task'
|
15
|
+
|
16
|
+
RuboCop::RakeTask.new
|
17
|
+
|
18
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
if RUBY_VERSION <= '3.1'
|
4
|
+
puts 'This example requires Ruby 3.1 or higher.'
|
5
|
+
exit! 1
|
6
|
+
end
|
7
|
+
|
8
|
+
# Usage:
|
9
|
+
#
|
10
|
+
# rake DISABLE_TRANSITIONS=t
|
11
|
+
# rake DISABLE_LISTENER=t
|
12
|
+
#
|
13
|
+
# rake HIDE_GIVEN_AND_CONTINUE=t
|
14
|
+
#
|
15
|
+
# rake BREAK_ACCOUNT_CREATION=t
|
16
|
+
# rake BREAK_USER_CREATION=t
|
17
|
+
# rake BREAK_USER_TOKEN_CREATION=t
|
18
|
+
#
|
19
|
+
# rake BREAK_ACCOUNT_CREATION=t HIDE_GIVEN_AND_CONTINUE=t
|
20
|
+
task default: %i[bcdd_result_transitions]
|
21
|
+
|
22
|
+
desc 'creates an account and an owner user through BCDD::Result'
|
23
|
+
task :bcdd_result_transitions do
|
24
|
+
require_relative 'config'
|
25
|
+
|
26
|
+
BCDD::Result.configuration do |config|
|
27
|
+
config.feature.disable!(:transitions) if ENV['DISABLE_TRANSITIONS']
|
28
|
+
|
29
|
+
unless ENV['DISABLE_LISTENER']
|
30
|
+
# config.transitions.listener = BCDD::Result::TransitionsRecord::Listener
|
31
|
+
config.transitions.listener = BCDD::Result::Transitions::Listeners[
|
32
|
+
TransitionsListener::Stdout,
|
33
|
+
BCDD::Result::TransitionsRecord::Listener
|
34
|
+
]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
result = nil
|
39
|
+
|
40
|
+
bench = Benchmark.measure do
|
41
|
+
result = Account::OwnerCreation.new.call(
|
42
|
+
owner: {
|
43
|
+
name: "\tJohn Doe \n",
|
44
|
+
email: ' JOHN.doe@email.com',
|
45
|
+
password: '123123123',
|
46
|
+
password_confirmation: '123123123'
|
47
|
+
}
|
48
|
+
)
|
49
|
+
rescue RuntimeBreaker::Interruption => e
|
50
|
+
nil
|
51
|
+
end
|
52
|
+
|
53
|
+
puts "\nBCDD::Result::TransitionsRecord.count: #{BCDD::Result::TransitionsRecord.count}"
|
54
|
+
|
55
|
+
puts "\nBenchmark: #{bench}"
|
56
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Account::Member < ActiveRecord::Base
|
4
|
+
self.table_name = 'account_members'
|
5
|
+
|
6
|
+
enum role: { owner: 0, admin: 1, contributor: 2 }
|
7
|
+
|
8
|
+
belongs_to :user, inverse_of: :memberships
|
9
|
+
belongs_to :account, inverse_of: :memberships
|
10
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Account
|
4
|
+
class OwnerCreation < ::BCDD::Process
|
5
|
+
include BCDD::Result::RollbackOnFailure
|
6
|
+
|
7
|
+
input do
|
8
|
+
attribute :uuid, contract: :is_uuid, default: -> { ::SecureRandom.uuid }, normalize: -> { _1.strip.downcase }
|
9
|
+
attribute :owner, type: ::Hash, validate: :is_present
|
10
|
+
end
|
11
|
+
|
12
|
+
output do
|
13
|
+
Failure(
|
14
|
+
invalid_owner: ::Hash,
|
15
|
+
invalid_account: :errors_by_attribute,
|
16
|
+
)
|
17
|
+
|
18
|
+
user = contract[::User] & :is_persisted
|
19
|
+
account = contract[::Account] & :is_persisted
|
20
|
+
|
21
|
+
Success(account_owner_created: { account:, user: })
|
22
|
+
end
|
23
|
+
|
24
|
+
def call(**input)
|
25
|
+
Given(input)
|
26
|
+
.then { |result|
|
27
|
+
rollback_on_failure {
|
28
|
+
result
|
29
|
+
.and_then(:create_owner)
|
30
|
+
.and_then(:create_account)
|
31
|
+
.and_then(:link_owner_to_account)
|
32
|
+
}
|
33
|
+
}.and_expose(:account_owner_created, %i[account user])
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def create_owner(owner:, **)
|
39
|
+
::User::Creation.call(**owner).handle do |on|
|
40
|
+
on.success { |output| Continue(user: output[:user]) }
|
41
|
+
on.failure { |output| Failure(:invalid_owner, **output) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def create_account(uuid:, **)
|
46
|
+
::RuntimeBreaker.try_to_interrupt(env: 'BREAK_ACCOUNT_CREATION')
|
47
|
+
|
48
|
+
account = ::Account.create(uuid:)
|
49
|
+
|
50
|
+
account.persisted? ? Continue(account:) : Failure(:invalid_account, **account.errors.messages)
|
51
|
+
end
|
52
|
+
|
53
|
+
def link_owner_to_account(account:, user:, **)
|
54
|
+
Member.create!(account:, user:, role: :owner)
|
55
|
+
|
56
|
+
Continue()
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Account < ActiveRecord::Base
|
4
|
+
has_many :memberships, inverse_of: :account, dependent: :destroy, class_name: '::Account::Member'
|
5
|
+
has_many :users, through: :memberships, inverse_of: :accounts
|
6
|
+
|
7
|
+
where_ownership = -> { where(account_members: {role: :owner}) }
|
8
|
+
|
9
|
+
has_one :ownership, where_ownership, dependent: nil, inverse_of: :account, class_name: '::Account::Member'
|
10
|
+
has_one :owner, through: :ownership, source: :user
|
11
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class User
|
4
|
+
class Creation < ::BCDD::Process
|
5
|
+
include BCDD::Result::RollbackOnFailure
|
6
|
+
|
7
|
+
input do
|
8
|
+
attribute :uuid, contract: :is_uuid, default: -> { ::SecureRandom.uuid }, normalize: -> { _1.strip.downcase }
|
9
|
+
attribute :name, contract: :is_str, normalize: -> { _1.strip.gsub(/\s+/, ' ') }
|
10
|
+
attribute :email, contract: :is_email, normalize: -> { _1.strip.downcase }
|
11
|
+
attribute :password, contract: :is_password
|
12
|
+
attribute :password_confirmation, contract: :is_password
|
13
|
+
end
|
14
|
+
|
15
|
+
output do
|
16
|
+
Failure(
|
17
|
+
invalid_user: :errors_by_attribute,
|
18
|
+
email_already_taken: :empty_hash,
|
19
|
+
)
|
20
|
+
|
21
|
+
user = contract[::User] & :is_persisted
|
22
|
+
token = contract[Token] & :is_persisted
|
23
|
+
|
24
|
+
Success(user_created: { user:, token: })
|
25
|
+
end
|
26
|
+
|
27
|
+
def call(**input)
|
28
|
+
Given(input)
|
29
|
+
.and_then(:validate_email_uniqueness)
|
30
|
+
.then { |result|
|
31
|
+
rollback_on_failure {
|
32
|
+
result
|
33
|
+
.and_then(:create_user)
|
34
|
+
.and_then(:create_user_token)
|
35
|
+
}
|
36
|
+
}
|
37
|
+
.and_expose(:user_created, %i[user token])
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def validate_email_uniqueness(email:, **)
|
43
|
+
::User.exists?(email:) ? Failure(:email_already_taken) : Continue()
|
44
|
+
end
|
45
|
+
|
46
|
+
def create_user(uuid:, name:, email:, password:, password_confirmation:)
|
47
|
+
::RuntimeBreaker.try_to_interrupt(env: 'BREAK_USER_CREATION')
|
48
|
+
|
49
|
+
user = ::User.create(uuid:, name:, email:, password:, password_confirmation:)
|
50
|
+
|
51
|
+
user.persisted? ? Continue(user:) : Failure(:invalid_user, **user.errors.messages)
|
52
|
+
end
|
53
|
+
|
54
|
+
def create_user_token(user:, **)
|
55
|
+
Token::Creation.new.call(user: user).handle do |on|
|
56
|
+
on.success { |output| Continue(token: output[:token]) }
|
57
|
+
on.failure { raise 'Token creation failed' }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class User::Token
|
4
|
+
class Creation < ::BCDD::Process
|
5
|
+
input do
|
6
|
+
attribute :user, contract: contract[::User] & :is_persisted
|
7
|
+
attribute :executed_at, contract: contract[::Time], default: -> { ::Time.current }
|
8
|
+
end
|
9
|
+
|
10
|
+
output do
|
11
|
+
Failure(
|
12
|
+
token_already_exists: :empty_hash,
|
13
|
+
token_creation_failed: :errors_by_attribute
|
14
|
+
)
|
15
|
+
|
16
|
+
token = contract[User::Token] & :is_persisted
|
17
|
+
|
18
|
+
Success(token_created: { token: })
|
19
|
+
end
|
20
|
+
|
21
|
+
def call(**input)
|
22
|
+
Given(input)
|
23
|
+
.and_then(:validate_token_existence)
|
24
|
+
.and_then(:create_token)
|
25
|
+
.and_expose(:token_created, %i[token])
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def validate_token_existence(user:, **)
|
31
|
+
user.token.nil? ? Continue() : Failure(:token_already_exists)
|
32
|
+
end
|
33
|
+
|
34
|
+
def create_token(user:, executed_at:, **)
|
35
|
+
::RuntimeBreaker.try_to_interrupt(env: 'BREAK_USER_TOKEN_CREATION')
|
36
|
+
|
37
|
+
token = user.create_token(
|
38
|
+
access_token: ::SecureRandom.hex(24),
|
39
|
+
refresh_token: ::SecureRandom.hex(24),
|
40
|
+
access_token_expires_at: executed_at + 15.days,
|
41
|
+
refresh_token_expires_at: executed_at + 30.days
|
42
|
+
)
|
43
|
+
|
44
|
+
token.persisted? ? Continue(token:) : Failure(:token_creation_failed, **token.errors.messages)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class User < ActiveRecord::Base
|
4
|
+
has_secure_password
|
5
|
+
|
6
|
+
has_many :memberships, inverse_of: :user, dependent: :destroy, class_name: '::Account::Member'
|
7
|
+
has_many :accounts, through: :memberships, inverse_of: :users
|
8
|
+
|
9
|
+
where_ownership = -> { where(account_members: { role: :owner }) }
|
10
|
+
|
11
|
+
has_one :ownership, where_ownership, inverse_of: :user, class_name: '::Account::Member'
|
12
|
+
has_one :account, through: :ownership, inverse_of: :owner
|
13
|
+
|
14
|
+
has_one :token, inverse_of: :user, dependent: :destroy, class_name: '::User::Token'
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/inline'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(__dir__)
|
6
|
+
|
7
|
+
gemfile do
|
8
|
+
source 'https://rubygems.org'
|
9
|
+
|
10
|
+
gem 'sqlite3', '~> 1.7'
|
11
|
+
gem 'bcrypt', '~> 3.1.20'
|
12
|
+
gem 'activerecord', '~> 7.1', '>= 7.1.3', require: 'active_record'
|
13
|
+
gem 'bcdd-process', path: '../../'
|
14
|
+
end
|
15
|
+
|
16
|
+
require 'active_support/all'
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
BCDD::Result.config.then do |config|
|
4
|
+
config.addon.enable!(:continue)
|
5
|
+
|
6
|
+
config.constant_alias.enable!('BCDD::Context')
|
7
|
+
|
8
|
+
config.pattern_matching.disable!(:nil_as_valid_value_checking)
|
9
|
+
|
10
|
+
# config.feature.disable!(:expectations) if Rails.env.production?
|
11
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/inline'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(__dir__)
|
6
|
+
|
7
|
+
require_relative 'config/boot'
|
8
|
+
require_relative 'config/initializers/bcdd'
|
9
|
+
|
10
|
+
require 'db/setup'
|
11
|
+
|
12
|
+
require 'lib/bcdd/result/rollback_on_failure'
|
13
|
+
require 'lib/bcdd/result/transitions_record'
|
14
|
+
require 'lib/bcdd/contracts'
|
15
|
+
require 'lib/runtime_breaker'
|
16
|
+
|
17
|
+
module TransitionsListener
|
18
|
+
require 'lib/transitions_listener/stdout'
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'app/models/account'
|
22
|
+
require 'app/models/account/member'
|
23
|
+
require 'app/models/user'
|
24
|
+
require 'app/models/user/token'
|
25
|
+
|
26
|
+
require 'app/models/account/owner_creation'
|
27
|
+
require 'app/models/user/token/creation'
|
28
|
+
require 'app/models/user/creation'
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/all'
|
4
|
+
|
5
|
+
ActiveRecord::Base.establish_connection(
|
6
|
+
host: 'localhost',
|
7
|
+
adapter: 'sqlite3',
|
8
|
+
database: ':memory:'
|
9
|
+
)
|
10
|
+
|
11
|
+
ActiveRecord::Schema.define do
|
12
|
+
suppress_messages do
|
13
|
+
create_table :bcdd_result_transitions do |t|
|
14
|
+
t.string :root_name, null: false, index: true
|
15
|
+
t.string :trace_id, index: true
|
16
|
+
t.integer :version, null: false
|
17
|
+
t.integer :duration, null: false, index: true
|
18
|
+
t.json :ids_tree, null: false, default: []
|
19
|
+
t.json :ids_matrix, null: false, default: {}
|
20
|
+
t.json :records, null: false, default: []
|
21
|
+
|
22
|
+
t.timestamps
|
23
|
+
end
|
24
|
+
|
25
|
+
create_table :accounts do |t|
|
26
|
+
t.string :uuid, null: false, index: {unique: true}
|
27
|
+
|
28
|
+
t.timestamps
|
29
|
+
end
|
30
|
+
|
31
|
+
create_table :users do |t|
|
32
|
+
t.string :uuid, null: false, index: {unique: true}
|
33
|
+
t.string :name, null: false
|
34
|
+
t.string :email, null: false, index: {unique: true}
|
35
|
+
t.string :password_digest, null: false
|
36
|
+
|
37
|
+
t.timestamps
|
38
|
+
end
|
39
|
+
|
40
|
+
create_table :user_tokens do |t|
|
41
|
+
t.belongs_to :user, null: false, foreign_key: true, index: true
|
42
|
+
t.string :access_token, null: false
|
43
|
+
t.string :refresh_token, null: false
|
44
|
+
t.datetime :access_token_expires_at, null: false
|
45
|
+
t.datetime :refresh_token_expires_at, null: false
|
46
|
+
|
47
|
+
t.timestamps
|
48
|
+
end
|
49
|
+
|
50
|
+
create_table :account_members do |t|
|
51
|
+
t.integer :role, null: false, default: 0
|
52
|
+
t.belongs_to :user, null: false, foreign_key: true, index: true
|
53
|
+
t.belongs_to :account, null: false, foreign_key: true, index: true
|
54
|
+
|
55
|
+
t.timestamps
|
56
|
+
|
57
|
+
t.index %i[account_id role], unique: true, where: "(role = 0)"
|
58
|
+
t.index %i[account_id user_id], unique: true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BCDD::Contracts
|
4
|
+
HasSize = ->(min, max) { ->(val) { val.size.between?(min, max) or "must be >= #{min} and <= #{max} chars" } }
|
5
|
+
|
6
|
+
is_str = ::BCDD::Contract[::String]
|
7
|
+
is_uuid = ->(val) { val.match?(/\A[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}\z/) or '%p must be an UUID' }
|
8
|
+
is_email = ->(val) { val.match?(::URI::MailTo::EMAIL_REGEXP) or '%p must be an email' }
|
9
|
+
is_present = ->(val) { val.present? or '%p must be present' }
|
10
|
+
is_persisted = ->(val) { val.persisted? or '%p must be persisted' }
|
11
|
+
|
12
|
+
::BCDD::Contract.register(
|
13
|
+
is_str: is_str,
|
14
|
+
is_uuid: is_str & is_present & is_uuid,
|
15
|
+
is_email: is_str & is_present& is_email,
|
16
|
+
is_present: is_present,
|
17
|
+
is_password: is_str & is_present & HasSize[8, 72],
|
18
|
+
is_persisted: is_persisted
|
19
|
+
)
|
20
|
+
|
21
|
+
EmptyHash = ::BCDD::Contract[::Hash] & ->(value) { value.empty? }
|
22
|
+
ErrorMessages = ::BCDD::Contract[errors: [::String]]
|
23
|
+
ErrorsByAttribute = ::BCDD::Contract.pairs(::Symbol => [::String])
|
24
|
+
|
25
|
+
::BCDD::Contract.register(
|
26
|
+
empty_hash: EmptyHash,
|
27
|
+
error_messages: ErrorMessages,
|
28
|
+
errors_by_attribute: ErrorsByAttribute
|
29
|
+
)
|
30
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BCDD::Result::RollbackOnFailure
|
4
|
+
def rollback_on_failure(model: ::ActiveRecord::Base)
|
5
|
+
result = nil
|
6
|
+
|
7
|
+
model.transaction do
|
8
|
+
result = yield
|
9
|
+
|
10
|
+
raise ::ActiveRecord::Rollback if result.failure?
|
11
|
+
end
|
12
|
+
|
13
|
+
result
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class BCDD::Result::TransitionsRecord < ActiveRecord::Base
|
4
|
+
self.table_name = 'bcdd_result_transitions'
|
5
|
+
|
6
|
+
class Listener
|
7
|
+
include ::BCDD::Result::Transitions::Listener
|
8
|
+
|
9
|
+
def on_finish(transitions:)
|
10
|
+
metadata = transitions[:metadata]
|
11
|
+
root_name = transitions.dig(:records, 0, :root, :name) || 'Unknown'
|
12
|
+
|
13
|
+
records = transitions[:records].map do |record|
|
14
|
+
record.deep_transform_values do |value|
|
15
|
+
value.is_a?(::BCDD::Process) ? value.class.name : value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
BCDD::Result::TransitionsRecord.create(
|
20
|
+
root_name: root_name,
|
21
|
+
duration: metadata[:duration],
|
22
|
+
trace_id: metadata[:trace_id],
|
23
|
+
ids_tree: metadata[:ids_tree],
|
24
|
+
ids_matrix: metadata[:ids_matrix],
|
25
|
+
version: transitions[:version],
|
26
|
+
records: records
|
27
|
+
)
|
28
|
+
rescue ::StandardError => e
|
29
|
+
err = "#{e.message} (#{e.class}); Backtrace: #{e.backtrace.join(', ')}"
|
30
|
+
|
31
|
+
::Kernel.warn "Error on BCDD::Result::TransitionsRecord::Listener#on_finish: #{err}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuntimeBreaker
|
4
|
+
Interruption = Class.new(StandardError)
|
5
|
+
|
6
|
+
def self.try_to_interrupt(env:)
|
7
|
+
return unless String(ENV[env]).strip.start_with?(/1|t/)
|
8
|
+
|
9
|
+
raise Interruption, "Runtime breaker activated (#{env})"
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class TransitionsListener::Stdout
|
4
|
+
include BCDD::Result::Transitions::Listener
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@buffer = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def on_start(scope:)
|
11
|
+
scope => { id:, name:, desc: }
|
12
|
+
|
13
|
+
@buffer << [id, "##{id} #{name} - #{desc}".chomp('- ')]
|
14
|
+
end
|
15
|
+
|
16
|
+
def on_record(record:)
|
17
|
+
record => { current: { id: }, result: { kind:, type: } }
|
18
|
+
|
19
|
+
method_name = record.dig(:and_then, :method_name)
|
20
|
+
|
21
|
+
@buffer << [id, " * #{kind}(#{type}) from method: #{method_name}".chomp('from method: ')]
|
22
|
+
end
|
23
|
+
|
24
|
+
MapNestedMessages = ->(transitions, buffer, hide_given_and_continue) do
|
25
|
+
ids_matrix = transitions.dig(:metadata, :ids_matrix)
|
26
|
+
|
27
|
+
messages = buffer.filter_map { |(id, msg)| "#{' ' * ids_matrix[id].last}#{msg}" if ids_matrix[id] }
|
28
|
+
|
29
|
+
messages.reject! { _1.match?(/\(_(given|continue)_\)/) } if hide_given_and_continue
|
30
|
+
|
31
|
+
messages
|
32
|
+
end
|
33
|
+
|
34
|
+
def on_finish(transitions:)
|
35
|
+
messages = MapNestedMessages[transitions, @buffer, ENV['HIDE_GIVEN_AND_CONTINUE']]
|
36
|
+
|
37
|
+
puts messages.join("\n")
|
38
|
+
end
|
39
|
+
|
40
|
+
def before_interruption(exception:, transitions:)
|
41
|
+
messages = MapNestedMessages[transitions, @buffer, ENV['HIDE_GIVEN_AND_CONTINUE']]
|
42
|
+
|
43
|
+
puts messages.join("\n")
|
44
|
+
|
45
|
+
bc = ::ActiveSupport::BacktraceCleaner.new
|
46
|
+
bc.add_filter { |line| line.gsub(__dir__.sub('/lib', ''), '').sub(/\A\//, '')}
|
47
|
+
bc.add_silencer { |line| /lib\/bcdd\/result/.match?(line) }
|
48
|
+
bc.add_silencer { |line| line.include?(RUBY_VERSION) }
|
49
|
+
|
50
|
+
backtrace = bc.clean(exception.backtrace)
|
51
|
+
|
52
|
+
puts "\nException: #{exception.message} (#{exception.class}); Backtrace: #{backtrace.join(", ")}"
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BCDD
|
4
|
+
class Process
|
5
|
+
module Caller
|
6
|
+
class NullContract
|
7
|
+
attr_reader :value
|
8
|
+
|
9
|
+
def initialize(value)
|
10
|
+
@value = value
|
11
|
+
end
|
12
|
+
|
13
|
+
def invalid?
|
14
|
+
false
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
PrepareInputs = ->(spec, input) do
|
19
|
+
spec.each_with_object({}) do |(name, options), result|
|
20
|
+
value = input.fetch(name) do
|
21
|
+
if options.key?(:default)
|
22
|
+
default = options[:default]
|
23
|
+
|
24
|
+
default.is_a?(::Proc) ? default.call : default
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
value = options[:normalize].call(value) if options.key?(:normalize)
|
29
|
+
|
30
|
+
result[name] = options.key?(:contract) ? options[:contract][value] : NullContract.new(value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def call(**inputs)
|
35
|
+
prepared_input = PrepareInputs[self.class.__input__, inputs]
|
36
|
+
|
37
|
+
Result.transitions(name: self.class.name) do
|
38
|
+
if self.class.__input_contract__
|
39
|
+
invalid_input = prepared_input.select { |_key, value| value.invalid? }
|
40
|
+
|
41
|
+
return Failure(:invalid_input, **invalid_input.transform_values(&:errors)) unless invalid_input.empty?
|
42
|
+
end
|
43
|
+
|
44
|
+
super(**prepared_input.transform_values!(&:value))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BCDD
|
4
|
+
class Process
|
5
|
+
class InputSpec
|
6
|
+
module MapContract
|
7
|
+
def self.[](options)
|
8
|
+
required = options.fetch(:required, true)
|
9
|
+
|
10
|
+
if options.key?(:contract) || options.key?(:type) || options.key?(:validate)
|
11
|
+
resolve(options).then { required ? _1 : (_1 | nil) }
|
12
|
+
elsif required
|
13
|
+
Contracts::CannotBeNil
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.resolve(options)
|
18
|
+
return ::BCDD::Contract[options[:contract]] if options.key?(:contract)
|
19
|
+
|
20
|
+
type = ::BCDD::Contract.unit(options[:type]) if options.key?(:type)
|
21
|
+
validate = ::BCDD::Contract.unit(options[:validate]) if options.key?(:validate)
|
22
|
+
|
23
|
+
type && validate ? (type & validate) : type || validate
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
MapDefault = ->(options) do
|
28
|
+
value = options[:default]
|
29
|
+
|
30
|
+
return value unless value.is_a?(Proc)
|
31
|
+
return value if value.lambda? && value.arity.zero?
|
32
|
+
|
33
|
+
raise ArgumentError, 'Default value must be a lambda with zero arity'
|
34
|
+
end
|
35
|
+
|
36
|
+
MapNormalize = ->(options) do
|
37
|
+
value = options[:normalize]
|
38
|
+
|
39
|
+
return value if value.is_a?(Proc) && value.lambda? && value.arity == 1
|
40
|
+
|
41
|
+
raise ArgumentError, 'normalize value must be a lambda with one arity'
|
42
|
+
end
|
43
|
+
|
44
|
+
# :nodoc:
|
45
|
+
attr_reader :__result__
|
46
|
+
|
47
|
+
def initialize
|
48
|
+
@__result__ = {}
|
49
|
+
end
|
50
|
+
|
51
|
+
def attribute(name, **options)
|
52
|
+
name.is_a?(Symbol) or raise ArgumentError, "#{name.inspect} must be a Symbol"
|
53
|
+
|
54
|
+
if options.key?(:contract) && (options.key?(:type) || options.key?(:validate))
|
55
|
+
raise ArgumentError, 'Cannot specify both :contract and (:type or :validate)'
|
56
|
+
end
|
57
|
+
|
58
|
+
spec = {}
|
59
|
+
spec[:default] = MapDefault[options] if options.key?(:default)
|
60
|
+
spec[:normalize] = MapNormalize[options] if options.key?(:normalize)
|
61
|
+
|
62
|
+
MapContract[options].then { spec[:contract] = _1 if _1 }
|
63
|
+
|
64
|
+
__result__[name] = spec
|
65
|
+
end
|
66
|
+
|
67
|
+
def contract
|
68
|
+
BCDD::Contract
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BCDD
|
4
|
+
class Process
|
5
|
+
class OutputSpec
|
6
|
+
def initialize
|
7
|
+
@success = {}
|
8
|
+
@failure = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def Success(**spec)
|
12
|
+
@success = spec.transform_values(&BCDD::Contract)
|
13
|
+
end
|
14
|
+
|
15
|
+
def Failure(**spec)
|
16
|
+
@failure = spec.transform_values(&BCDD::Contract)
|
17
|
+
end
|
18
|
+
|
19
|
+
# :nodoc:
|
20
|
+
def __result__
|
21
|
+
{ success: @success, failure: @failure }
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def contract
|
27
|
+
::BCDD::Contract
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/bcdd/process.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bcdd/result'
|
4
|
+
require 'bcdd/contract'
|
5
|
+
|
6
|
+
require_relative 'process/version'
|
7
|
+
require_relative 'process/caller'
|
8
|
+
require_relative 'process/input_spec'
|
9
|
+
require_relative 'process/output_spec'
|
10
|
+
|
11
|
+
module BCDD
|
12
|
+
module Contracts
|
13
|
+
CannotBeNil = BCDD::Contract[-> { _1.nil? and 'cannot be nil' }]
|
14
|
+
end
|
15
|
+
|
16
|
+
class Process
|
17
|
+
class << self
|
18
|
+
attr_reader :__input__, :__input_contract__, :__output__
|
19
|
+
|
20
|
+
def input(&block)
|
21
|
+
return @__input__ if defined?(@__input__)
|
22
|
+
|
23
|
+
spec = InputSpec.new
|
24
|
+
spec.instance_eval(&block)
|
25
|
+
spec.__result__.transform_values!(&:freeze).freeze
|
26
|
+
|
27
|
+
@__input_contract__ = spec.__result__.any? { |_key, value| value.key?(:contract) }
|
28
|
+
@__input__ = spec.__result__
|
29
|
+
end
|
30
|
+
|
31
|
+
def output(expectations: true, &block)
|
32
|
+
@__output__ and raise ArgumentError, 'outputs already defined'
|
33
|
+
|
34
|
+
config = { addon: { given: true, continue: true } }
|
35
|
+
|
36
|
+
if expectations
|
37
|
+
spec = OutputSpec.new
|
38
|
+
spec.instance_eval(&block)
|
39
|
+
|
40
|
+
output = spec.__result__
|
41
|
+
success = output[:success]
|
42
|
+
failure = output.fetch(:failure, {}).merge(invalid_input: ::Hash)
|
43
|
+
|
44
|
+
include(Result::Context::Expectations.mixin(config: config, success: success, failure: failure))
|
45
|
+
else
|
46
|
+
include(Result::Context.mixin(config: config))
|
47
|
+
end
|
48
|
+
|
49
|
+
@__output__ = { expectations: expectations }.freeze
|
50
|
+
end
|
51
|
+
|
52
|
+
def inherited(subclass)
|
53
|
+
subclass.prepend(Caller)
|
54
|
+
end
|
55
|
+
|
56
|
+
def call(**input)
|
57
|
+
new.call(**input)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/bcdd-process.rb
ADDED
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bcdd-process
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rodrigo Serradura
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-02-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bcdd-contract
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bcdd-result
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.13.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.13.0
|
41
|
+
description: Write reliable, self-documented, and self-observable business processes
|
42
|
+
in Ruby.
|
43
|
+
email:
|
44
|
+
- rodrigo.serradura@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- ".rubocop_todo.yml"
|
51
|
+
- CHANGELOG.md
|
52
|
+
- CODE_OF_CONDUCT.md
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- examples/business_processes/Rakefile
|
57
|
+
- examples/business_processes/app/models/account.rb
|
58
|
+
- examples/business_processes/app/models/account/member.rb
|
59
|
+
- examples/business_processes/app/models/account/owner_creation.rb
|
60
|
+
- examples/business_processes/app/models/user.rb
|
61
|
+
- examples/business_processes/app/models/user/creation.rb
|
62
|
+
- examples/business_processes/app/models/user/token.rb
|
63
|
+
- examples/business_processes/app/models/user/token/creation.rb
|
64
|
+
- examples/business_processes/config.rb
|
65
|
+
- examples/business_processes/config/boot.rb
|
66
|
+
- examples/business_processes/config/initializers/bcdd.rb
|
67
|
+
- examples/business_processes/db/setup.rb
|
68
|
+
- examples/business_processes/lib/bcdd/contracts.rb
|
69
|
+
- examples/business_processes/lib/bcdd/result/rollback_on_failure.rb
|
70
|
+
- examples/business_processes/lib/bcdd/result/transitions_record.rb
|
71
|
+
- examples/business_processes/lib/runtime_breaker.rb
|
72
|
+
- examples/business_processes/lib/transitions_listener/stdout.rb
|
73
|
+
- lib/bcdd-process.rb
|
74
|
+
- lib/bcdd/process.rb
|
75
|
+
- lib/bcdd/process/caller.rb
|
76
|
+
- lib/bcdd/process/input_spec.rb
|
77
|
+
- lib/bcdd/process/output_spec.rb
|
78
|
+
- lib/bcdd/process/version.rb
|
79
|
+
- sig/bcdd/process.rbs
|
80
|
+
homepage: https://github.com/b-cdd/process
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata:
|
84
|
+
allowed_push_host: https://rubygems.org
|
85
|
+
homepage_uri: https://github.com/b-cdd/process
|
86
|
+
source_code_uri: https://github.com/b-cdd/process
|
87
|
+
changelog_uri: https://github.com/b-cdd/process/blob/main/CHANGELOG.md
|
88
|
+
rubygems_mfa_required: 'true'
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: 2.7.0
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubygems_version: 3.5.3
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Write reliable, self-documented, and self-observable business processes in
|
108
|
+
Ruby.
|
109
|
+
test_files: []
|