bcdd-process 0.2.0 → 0.3.0
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 +4 -4
- data/CHANGELOG.md +25 -1
- data/README.md +6 -6
- data/examples/business_processes/app/models/account/owner_creation.rb +13 -16
- data/examples/business_processes/app/models/user/creation.rb +6 -6
- data/examples/business_processes/app/models/user/token/creation.rb +1 -3
- data/examples/business_processes/lib/bcdd/contracts.rb +6 -6
- data/lib/bcdd/contract/null.rb +26 -0
- data/lib/bcdd/contracts.rb +21 -0
- data/lib/bcdd/process/caller.rb +1 -13
- data/lib/bcdd/process/input_spec.rb +4 -10
- data/lib/bcdd/process/version.rb +1 -1
- data/lib/bcdd/process.rb +3 -4
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fc44df9862fb1cc767ccf9c30a2449c9757beededc5362a827f0910cce3502f
|
4
|
+
data.tar.gz: ac170094d857f6dc057675083ebf5224620a423d8e9b09e50b5ab31f0442802b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d55396e492e307ff8c62e54b875bc4476c34918a4f0d722f1f8ef15a1a78ca12f1c984b4d129fa9957e237d431a923fd05325ff22eae58aca7d8f34ea7a06ab5
|
7
|
+
data.tar.gz: b81991dc5c9ee80191e5045824a22a91f5bd0635ba584585402d56e793fd5d2026e007e3288c02a7e2a61dede9b2436635151d97517294e4bd45d102b4885127
|
data/CHANGELOG.md
CHANGED
@@ -1,11 +1,35 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.3.0] - 2024-02-03
|
4
|
+
|
5
|
+
### Added
|
6
|
+
|
7
|
+
- Allow the usage of `proc(&)` with the input `normalize:` property.
|
8
|
+
```ruby
|
9
|
+
attribute :name, type: String, normalize: proc(&:to_s) >> proc(&:strip)
|
10
|
+
```
|
11
|
+
|
12
|
+
### Changed
|
13
|
+
|
14
|
+
- **BREAKING**: Remove `validate:` input property and allow the composition of `contract:` + `type:` properties.
|
15
|
+
```ruby
|
16
|
+
# Before
|
17
|
+
attribute :name, type: String, validate: :is_present
|
18
|
+
|
19
|
+
# After
|
20
|
+
attribute :name, type: String, contract: is_present
|
21
|
+
```
|
22
|
+
|
3
23
|
## [0.2.0] - 2024-02-01
|
4
24
|
|
5
|
-
|
25
|
+
### Changed
|
26
|
+
|
27
|
+
- **BREAKING**: Update gem's dependencies.
|
6
28
|
- bcdd-contract >= 0.1.0
|
7
29
|
- bcdd-result >= 0.3.0
|
8
30
|
|
9
31
|
## [0.1.0] - 2024-02-01
|
10
32
|
|
33
|
+
### Added
|
34
|
+
|
11
35
|
- Add `BCDD::Process` - Initial/POC release.
|
data/README.md
CHANGED
@@ -37,7 +37,7 @@ class User
|
|
37
37
|
include BCDD::Result::RollbackOnFailure
|
38
38
|
|
39
39
|
input do
|
40
|
-
attribute :uuid, contract: :is_uuid,
|
40
|
+
attribute :uuid, contract: :is_uuid, normalize: -> { _1.strip.downcase }, default: -> { ::SecureRandom.uuid }
|
41
41
|
attribute :name, contract: :is_str, normalize: -> { _1.strip.gsub(/\s+/, ' ') }
|
42
42
|
attribute :email, contract: :is_email, normalize: -> { _1.strip.downcase }
|
43
43
|
attribute :password, contract: :is_password
|
@@ -47,13 +47,13 @@ class User
|
|
47
47
|
output do
|
48
48
|
Failure(
|
49
49
|
invalid_user: :errors_by_attribute,
|
50
|
-
email_already_taken: :empty_hash
|
50
|
+
email_already_taken: :empty_hash
|
51
51
|
)
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
Success user_created: {
|
54
|
+
user: contract[::User] & :is_persisted,
|
55
|
+
token: contract[Token] & :is_persisted
|
56
|
+
}
|
57
57
|
end
|
58
58
|
|
59
59
|
def call(**input)
|
@@ -5,32 +5,29 @@ class Account
|
|
5
5
|
include BCDD::Result::RollbackOnFailure
|
6
6
|
|
7
7
|
input do
|
8
|
-
attribute :uuid, contract: :is_uuid,
|
9
|
-
attribute :owner, type: ::Hash,
|
8
|
+
attribute :uuid, contract: :is_uuid, normalize: -> { _1.strip.downcase }, default: -> { ::SecureRandom.uuid }
|
9
|
+
attribute :owner, type: ::Hash, contract: :is_present
|
10
10
|
end
|
11
11
|
|
12
12
|
output do
|
13
13
|
Failure(
|
14
14
|
invalid_owner: ::Hash,
|
15
|
-
invalid_account: :errors_by_attribute
|
15
|
+
invalid_account: :errors_by_attribute
|
16
16
|
)
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
Success account_owner_created: {
|
19
|
+
user: contract[::User] & :is_persisted,
|
20
|
+
account: contract[::Account] & :is_persisted
|
21
|
+
}
|
22
22
|
end
|
23
23
|
|
24
24
|
def call(**input)
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
.and_then(:link_owner_to_account)
|
32
|
-
}
|
33
|
-
}.and_expose(:account_owner_created, %i[account user])
|
25
|
+
rollback_on_failure {
|
26
|
+
Given(input)
|
27
|
+
.and_then(:create_owner)
|
28
|
+
.and_then(:create_account)
|
29
|
+
.and_then(:link_owner_to_account)
|
30
|
+
}.and_expose(:account_owner_created, %i[account user])
|
34
31
|
end
|
35
32
|
|
36
33
|
private
|
@@ -5,7 +5,7 @@ class User
|
|
5
5
|
include BCDD::Result::RollbackOnFailure
|
6
6
|
|
7
7
|
input do
|
8
|
-
attribute :uuid, contract: :is_uuid,
|
8
|
+
attribute :uuid, contract: :is_uuid, normalize: -> { _1.strip.downcase }, default: -> { ::SecureRandom.uuid }
|
9
9
|
attribute :name, contract: :is_str, normalize: -> { _1.strip.gsub(/\s+/, ' ') }
|
10
10
|
attribute :email, contract: :is_email, normalize: -> { _1.strip.downcase }
|
11
11
|
attribute :password, contract: :is_password
|
@@ -15,13 +15,13 @@ class User
|
|
15
15
|
output do
|
16
16
|
Failure(
|
17
17
|
invalid_user: :errors_by_attribute,
|
18
|
-
email_already_taken: :empty_hash
|
18
|
+
email_already_taken: :empty_hash
|
19
19
|
)
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
Success user_created: {
|
22
|
+
user: contract[::User] & :is_persisted,
|
23
|
+
token: contract[Token] & :is_persisted
|
24
|
+
}
|
25
25
|
end
|
26
26
|
|
27
27
|
def call(**input)
|
@@ -13,9 +13,7 @@ class User::Token
|
|
13
13
|
token_creation_failed: :errors_by_attribute
|
14
14
|
)
|
15
15
|
|
16
|
-
token
|
17
|
-
|
18
|
-
Success(token_created: { token: })
|
16
|
+
Success token_created: { token: contract[User::Token] & :is_persisted }
|
19
17
|
end
|
20
18
|
|
21
19
|
def call(**input)
|
@@ -3,13 +3,13 @@
|
|
3
3
|
module BCDD::Contracts
|
4
4
|
HasSize = ->(min, max) { ->(val) { val.size.between?(min, max) or "must be >= #{min} and <= #{max} chars" } }
|
5
5
|
|
6
|
-
is_str =
|
6
|
+
is_str = contract[::String]
|
7
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
8
|
is_email = ->(val) { val.match?(::URI::MailTo::EMAIL_REGEXP) or '%p must be an email' }
|
9
9
|
is_present = ->(val) { val.present? or '%p must be present' }
|
10
10
|
is_persisted = ->(val) { val.persisted? or '%p must be persisted' }
|
11
11
|
|
12
|
-
|
12
|
+
register(
|
13
13
|
is_str: is_str,
|
14
14
|
is_uuid: is_str & is_present & is_uuid,
|
15
15
|
is_email: is_str & is_present& is_email,
|
@@ -18,11 +18,11 @@ module BCDD::Contracts
|
|
18
18
|
is_persisted: is_persisted
|
19
19
|
)
|
20
20
|
|
21
|
-
EmptyHash =
|
22
|
-
ErrorMessages =
|
23
|
-
ErrorsByAttribute =
|
21
|
+
EmptyHash = contract[::Hash] & ->(value) { value.empty? }
|
22
|
+
ErrorMessages = contract[errors: [::String]]
|
23
|
+
ErrorsByAttribute = contract.pairs(::Symbol => [::String])
|
24
24
|
|
25
|
-
|
25
|
+
register(
|
26
26
|
empty_hash: EmptyHash,
|
27
27
|
error_messages: ErrorMessages,
|
28
28
|
errors_by_attribute: ErrorsByAttribute
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BCDD::Contract
|
4
|
+
# TODO: Move to bcdd-contract
|
5
|
+
module Null
|
6
|
+
class Checking
|
7
|
+
include Core::Checking
|
8
|
+
|
9
|
+
EMPTY_ARRAY = [].freeze
|
10
|
+
|
11
|
+
def initialize(_checker, value)
|
12
|
+
@value = value
|
13
|
+
@errors = EMPTY_ARRAY
|
14
|
+
end
|
15
|
+
|
16
|
+
def errors_message
|
17
|
+
''
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# TODO: Move to bcdd-contract
|
23
|
+
def self.null(value)
|
24
|
+
Null::Checking.new(nil, value)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# TODO: Move to bcdd-contract
|
4
|
+
module BCDD::Contracts
|
5
|
+
class << self
|
6
|
+
private
|
7
|
+
|
8
|
+
def contract
|
9
|
+
::BCDD::Contract
|
10
|
+
end
|
11
|
+
|
12
|
+
def register(**kargs)
|
13
|
+
# TODO: Remove this method and make the use open BCDD::Contracts module to register contracts
|
14
|
+
::BCDD::Contract.register(**kargs)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
NotNil = contract[-> { _1.nil? and 'cannot be nil' }]
|
19
|
+
|
20
|
+
register(not_nil: NotNil)
|
21
|
+
end
|
data/lib/bcdd/process/caller.rb
CHANGED
@@ -3,18 +3,6 @@
|
|
3
3
|
module BCDD
|
4
4
|
class Process
|
5
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
6
|
PrepareInputs = ->(spec, input) do
|
19
7
|
spec.each_with_object({}) do |(name, options), result|
|
20
8
|
value = input.fetch(name) do
|
@@ -27,7 +15,7 @@ module BCDD
|
|
27
15
|
|
28
16
|
value = options[:normalize].call(value) if options.key?(:normalize)
|
29
17
|
|
30
|
-
result[name] = options.key?(:contract) ? options[:contract][value] :
|
18
|
+
result[name] = options.key?(:contract) ? options[:contract][value] : Contract.null(value)
|
31
19
|
end
|
32
20
|
end
|
33
21
|
|
@@ -10,17 +10,15 @@ module BCDD
|
|
10
10
|
if options.key?(:contract) || options.key?(:type) || options.key?(:validate)
|
11
11
|
resolve(options).then { required ? _1 : (_1 | nil) }
|
12
12
|
elsif required
|
13
|
-
Contracts::
|
13
|
+
Contracts::NotNil
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
def self.resolve(options)
|
18
|
-
return ::BCDD::Contract[options[:contract]] if options.key?(:contract)
|
19
|
-
|
20
18
|
type = ::BCDD::Contract.unit(options[:type]) if options.key?(:type)
|
21
|
-
|
19
|
+
contract = ::BCDD::Contract[options[:contract]] if options.key?(:contract)
|
22
20
|
|
23
|
-
type &&
|
21
|
+
type && contract ? (type & contract) : type || contract
|
24
22
|
end
|
25
23
|
end
|
26
24
|
|
@@ -36,7 +34,7 @@ module BCDD
|
|
36
34
|
MapNormalize = ->(options) do
|
37
35
|
value = options[:normalize]
|
38
36
|
|
39
|
-
return value if value.is_a?(Proc) && value.lambda?
|
37
|
+
return value if value.is_a?(Proc) && value.lambda?
|
40
38
|
|
41
39
|
raise ArgumentError, 'normalize value must be a lambda with one arity'
|
42
40
|
end
|
@@ -51,10 +49,6 @@ module BCDD
|
|
51
49
|
def attribute(name, **options)
|
52
50
|
name.is_a?(Symbol) or raise ArgumentError, "#{name.inspect} must be a Symbol"
|
53
51
|
|
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
52
|
spec = {}
|
59
53
|
spec[:default] = MapDefault[options] if options.key?(:default)
|
60
54
|
spec[:normalize] = MapNormalize[options] if options.key?(:normalize)
|
data/lib/bcdd/process/version.rb
CHANGED
data/lib/bcdd/process.rb
CHANGED
@@ -3,16 +3,15 @@
|
|
3
3
|
require 'bcdd/result'
|
4
4
|
require 'bcdd/contract'
|
5
5
|
|
6
|
+
require_relative 'contracts'
|
7
|
+
require_relative 'contract/null'
|
8
|
+
|
6
9
|
require_relative 'process/version'
|
7
10
|
require_relative 'process/caller'
|
8
11
|
require_relative 'process/input_spec'
|
9
12
|
require_relative 'process/output_spec'
|
10
13
|
|
11
14
|
module BCDD
|
12
|
-
module Contracts
|
13
|
-
CannotBeNil = BCDD::Contract[-> { _1.nil? and 'cannot be nil' }]
|
14
|
-
end
|
15
|
-
|
16
15
|
class Process
|
17
16
|
class << self
|
18
17
|
attr_reader :__input__, :__input_contract__, :__output__
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bcdd-process
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-02-
|
11
|
+
date: 2024-02-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bcdd-contract
|
@@ -71,6 +71,8 @@ files:
|
|
71
71
|
- examples/business_processes/lib/runtime_breaker.rb
|
72
72
|
- examples/business_processes/lib/transitions_listener/stdout.rb
|
73
73
|
- lib/bcdd-process.rb
|
74
|
+
- lib/bcdd/contract/null.rb
|
75
|
+
- lib/bcdd/contracts.rb
|
74
76
|
- lib/bcdd/process.rb
|
75
77
|
- lib/bcdd/process/caller.rb
|
76
78
|
- lib/bcdd/process/input_spec.rb
|