ucasy 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +106 -3
- data/lib/ucasy/flow.rb +28 -7
- data/lib/ucasy/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36d451f98af67ec26170e55b1ad2799192ae80d852b5cf1436f3858b8d4718ad
|
4
|
+
data.tar.gz: 06e1bf635c4d2fa3a7a31b7d304f13029a37bab989e6bc32429d264cd324d2dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43cdad21e2a991eaa28285905828ba6bec0fe52ef5a1a22bdf8b3969490af67945c56bbfd2b3ba29bf640f170fa5fdb03ba3781fdcff08fbee060602af296aaa
|
7
|
+
data.tar.gz: 6e7fd420a5b4f4d46fbf79afd9797f24ee590025b3dc84830740ada4d2b8fd7c14a27beb05119a32841115817e1d238ee29d1293b36885db0de229493542811e
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@ Description soon!
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ru
|
10
|
-
gem "ucasy", "~> 0.0
|
10
|
+
gem "ucasy", "~> 0.2.0"
|
11
11
|
```
|
12
12
|
|
13
13
|
And then execute:
|
@@ -22,14 +22,117 @@ Or install it yourself as:
|
|
22
22
|
bundle add ucasy
|
23
23
|
```
|
24
24
|
|
25
|
-
##
|
25
|
+
## Create base file
|
26
26
|
|
27
|
-
You can use a Rails generator to create setup
|
27
|
+
You can use a Rails generator to create setup `UseCaseBase` file:
|
28
28
|
|
29
29
|
```sh
|
30
30
|
rails g ucasy:install
|
31
31
|
```
|
32
32
|
|
33
|
+
This will create `app/use_cases/use_case_base.rb`
|
34
|
+
|
35
|
+
```rb
|
36
|
+
class UseCaseBase < Ucasy::Base
|
37
|
+
class Flow < Ucasy::Flow
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
## Basic usage
|
43
|
+
|
44
|
+
You can create a simple use case to `app/use_cases/authenticate_user.rb`:
|
45
|
+
|
46
|
+
```rb
|
47
|
+
class AuthenticateUser < UseCaseBase
|
48
|
+
def before
|
49
|
+
context.user = User.find_by(login: context.login)
|
50
|
+
end
|
51
|
+
|
52
|
+
def call
|
53
|
+
return if context.user.valid_password?(context.password)
|
54
|
+
|
55
|
+
context.fail!(
|
56
|
+
status: :unprocessable_entity,
|
57
|
+
message: "User not found",
|
58
|
+
errors: context.user.errors
|
59
|
+
)
|
60
|
+
end
|
61
|
+
|
62
|
+
def after
|
63
|
+
UserMailer.with(user: context.user).login_warning.perform_later
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
## Using methods
|
69
|
+
|
70
|
+
There is some methods to ensure arguments for each use case:
|
71
|
+
|
72
|
+
### required_attributes
|
73
|
+
|
74
|
+
```rb
|
75
|
+
class AuthenticateUser < UseCaseBase
|
76
|
+
required_attributes(:login, :password)
|
77
|
+
|
78
|
+
def call
|
79
|
+
context.login
|
80
|
+
context.password
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
The method `required_attributes` raises an error if `login` or `password` is blank
|
86
|
+
|
87
|
+
|
88
|
+
### validate
|
89
|
+
|
90
|
+
You can use ActiveModel or any class that respond to `valid?`, `errors`, `present?` and accepts a hash as arguments to validate you payload.
|
91
|
+
|
92
|
+
Create a class
|
93
|
+
|
94
|
+
```rb
|
95
|
+
class AuthenticateUser < UseCaseBase
|
96
|
+
validate(AuthenticateUserValidation)
|
97
|
+
|
98
|
+
def call
|
99
|
+
context.login
|
100
|
+
context.password
|
101
|
+
end
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
Adds a validation
|
106
|
+
|
107
|
+
```rb
|
108
|
+
class AuthenticateUserValidation
|
109
|
+
include ActiveModel::Model
|
110
|
+
|
111
|
+
attr_accessor :login, :password
|
112
|
+
|
113
|
+
validates :login, presence: true
|
114
|
+
validates :password, presence: true
|
115
|
+
|
116
|
+
def to_context
|
117
|
+
{login:, :password} # this hash will be injected in use cases context
|
118
|
+
end
|
119
|
+
end
|
120
|
+
```
|
121
|
+
|
122
|
+
## Using Flow
|
123
|
+
|
124
|
+
You can also create a flow to organize your use cases:
|
125
|
+
|
126
|
+
```rb
|
127
|
+
class PlaceOrder < UseCaseBase::Flow
|
128
|
+
transactional # if any use case fails ActiveRecord will rollback transaction
|
129
|
+
|
130
|
+
validate(PlaceOrderValidation)
|
131
|
+
|
132
|
+
flow(ChargeCard, SendThankYou, FulfillOrder)
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
33
136
|
## Contributing
|
34
137
|
|
35
138
|
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ucasy.
|
data/lib/ucasy/flow.rb
CHANGED
@@ -1,22 +1,43 @@
|
|
1
1
|
module Ucasy
|
2
2
|
class Flow < Base
|
3
3
|
class << self
|
4
|
-
def
|
5
|
-
@
|
4
|
+
def flow(*ucases)
|
5
|
+
@ucases = ucases
|
6
6
|
end
|
7
|
-
alias_method :flow, :service_classes
|
8
7
|
|
9
|
-
def
|
10
|
-
@
|
8
|
+
def ucases
|
9
|
+
@ucases || []
|
10
|
+
end
|
11
|
+
|
12
|
+
def transactional
|
13
|
+
@transactional = true
|
14
|
+
end
|
15
|
+
|
16
|
+
def transactional?
|
17
|
+
@transactional || false
|
11
18
|
end
|
12
19
|
end
|
13
20
|
|
14
21
|
def call
|
15
|
-
self.class.
|
16
|
-
|
22
|
+
if self.class.transactional?
|
23
|
+
ActiveRecord::Base.transaction { execute }
|
24
|
+
else
|
25
|
+
execute
|
17
26
|
end
|
18
27
|
|
19
28
|
self
|
20
29
|
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def execute
|
34
|
+
self.class.ucases.each do |ucase|
|
35
|
+
service_class.call(context)
|
36
|
+
|
37
|
+
next unless self.class.transactional?
|
38
|
+
|
39
|
+
raise ActiveRecord::Rollback if failure?
|
40
|
+
end
|
41
|
+
end
|
21
42
|
end
|
22
43
|
end
|
data/lib/ucasy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ucasy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lavenda Software
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
requirements: []
|
63
|
-
rubygems_version: 3.5.
|
63
|
+
rubygems_version: 3.5.21
|
64
64
|
signing_key:
|
65
65
|
specification_version: 4
|
66
66
|
summary: Ucasy is just a quick test suite setup for Rails application
|