haku 1.3.1 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +19 -1
- data/CHANGELOG.md +11 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +26 -30
- data/LICENSE +1 -1
- data/README.md +150 -68
- data/lib/haku/core.rb +11 -3
- data/lib/haku/delayable.rb +51 -0
- data/lib/haku/eventable.rb +8 -15
- data/lib/haku/version.rb +2 -2
- data/lib/haku.rb +2 -1
- metadata +9 -12
- data/Appraisals +0 -11
- data/gemfiles/activesupport_6.0.gemfile +0 -14
- data/gemfiles/activesupport_6.1.gemfile +0 -14
- data/gemfiles/activesupport_7.0.gemfile +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 137f060c137bed529cdd82679e0880674ed6a6b5d62a9865bf5f52fb8c89c805
|
4
|
+
data.tar.gz: 8ca69778bad99f1cd7f2ea6a64fdc0784337741acbde926ff487f5bc2312ff67
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53a9a2ffdd2f719e2aad8878130a7a2359cd818947be00cedaf82477262cafab6431e357520fb14a738f9fbc80b790a42077d9a68f000c3fa769ffea45369c04
|
7
|
+
data.tar.gz: 56ec2ff6a811220f409ed65df974493ffb0adcb77b7eb50f9a114e63a3c79af4160728b66278126d78886892f6e35bb63d973a77d26895bca7f3612a1fbc4efc
|
data/.rubocop.yml
CHANGED
@@ -15,11 +15,29 @@ AllCops:
|
|
15
15
|
- "**/gemfiles/**/*"
|
16
16
|
- "**/Appraisals"
|
17
17
|
NewCops: enable
|
18
|
-
TargetRubyVersion: 2.
|
18
|
+
TargetRubyVersion: 2.7
|
19
19
|
|
20
20
|
Layout/SpaceAroundEqualsInParameterDefault:
|
21
21
|
EnforcedStyle: no_space
|
22
22
|
|
23
|
+
Metrics/ClassLength:
|
24
|
+
CountAsOne:
|
25
|
+
- array
|
26
|
+
- hash
|
27
|
+
- heredoc
|
28
|
+
|
29
|
+
Metrics/MethodLength:
|
30
|
+
CountAsOne:
|
31
|
+
- array
|
32
|
+
- hash
|
33
|
+
- heredoc
|
34
|
+
|
35
|
+
Metrics/ModuleLength:
|
36
|
+
CountAsOne:
|
37
|
+
- array
|
38
|
+
- hash
|
39
|
+
- heredoc
|
40
|
+
|
23
41
|
Style/Documentation:
|
24
42
|
Enabled: false
|
25
43
|
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [1.5.0] - 2023-01-25
|
4
|
+
* **Breaking**: Simplify Haku::Eventable module
|
5
|
+
|
6
|
+
## [1.4.0] - 2022-09-19
|
7
|
+
- Improve `Haku::Eventable` module to support more data sources for event properties
|
8
|
+
- Add `Haku::Delayable` module to execute service object in background
|
9
|
+
- Improve README
|
10
|
+
- **Breaking**: Require `activesupport` >= 6.1
|
11
|
+
- **Breaking**: Require Ruby >= 2.7
|
12
|
+
- Fix CI test invocation
|
13
|
+
|
3
14
|
## [1.3.1] - 2022-04-27
|
4
15
|
- Fix Haku::Controller
|
5
16
|
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,71 +1,67 @@
|
|
1
|
-
GIT
|
2
|
-
remote: https://github.com/excid3/appraisal.git
|
3
|
-
revision: 14855fc54ce88b42def871ce8bfd4259fbf06043
|
4
|
-
branch: fix-bundle-env
|
5
|
-
specs:
|
6
|
-
appraisal (2.4.1)
|
7
|
-
bundler
|
8
|
-
rake
|
9
|
-
thor (>= 0.14.0)
|
10
|
-
|
11
1
|
PATH
|
12
2
|
remote: .
|
13
3
|
specs:
|
14
|
-
haku (1.
|
15
|
-
activesupport (>= 6.
|
4
|
+
haku (1.5.0)
|
5
|
+
activesupport (>= 6.1, < 8.0)
|
16
6
|
|
17
7
|
GEM
|
18
8
|
remote: https://rubygems.org/
|
19
9
|
specs:
|
20
|
-
|
10
|
+
activejob (7.0.4.2)
|
11
|
+
activesupport (= 7.0.4.2)
|
12
|
+
globalid (>= 0.3.6)
|
13
|
+
activesupport (7.0.4.2)
|
21
14
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
22
15
|
i18n (>= 1.6, < 2)
|
23
16
|
minitest (>= 5.1)
|
24
17
|
tzinfo (~> 2.0)
|
25
18
|
ast (2.4.2)
|
26
19
|
coderay (1.1.3)
|
27
|
-
concurrent-ruby (1.
|
28
|
-
|
20
|
+
concurrent-ruby (1.2.0)
|
21
|
+
globalid (1.0.1)
|
22
|
+
activesupport (>= 5.0)
|
23
|
+
i18n (1.12.0)
|
29
24
|
concurrent-ruby (~> 1.0)
|
25
|
+
json (2.6.3)
|
30
26
|
method_source (1.0.0)
|
31
|
-
minitest (5.
|
27
|
+
minitest (5.17.0)
|
32
28
|
parallel (1.22.1)
|
33
|
-
parser (3.
|
29
|
+
parser (3.2.0.0)
|
34
30
|
ast (~> 2.4.1)
|
35
|
-
pry (0.14.
|
31
|
+
pry (0.14.2)
|
36
32
|
coderay (~> 1.1)
|
37
33
|
method_source (~> 1.0)
|
38
34
|
rainbow (3.1.1)
|
39
35
|
rake (13.0.6)
|
40
|
-
regexp_parser (2.2
|
36
|
+
regexp_parser (2.6.2)
|
41
37
|
rexml (3.2.5)
|
42
|
-
rubocop (1.
|
38
|
+
rubocop (1.44.1)
|
39
|
+
json (~> 2.3)
|
43
40
|
parallel (~> 1.10)
|
44
|
-
parser (>= 3.
|
41
|
+
parser (>= 3.2.0.0)
|
45
42
|
rainbow (>= 2.2.2, < 4.0)
|
46
43
|
regexp_parser (>= 1.8, < 3.0)
|
47
|
-
rexml
|
48
|
-
rubocop-ast (>= 1.
|
44
|
+
rexml (>= 3.2.5, < 4.0)
|
45
|
+
rubocop-ast (>= 1.24.1, < 2.0)
|
49
46
|
ruby-progressbar (~> 1.7)
|
50
|
-
unicode-display_width (>=
|
51
|
-
rubocop-ast (1.
|
47
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
48
|
+
rubocop-ast (1.24.1)
|
52
49
|
parser (>= 3.1.1.0)
|
53
|
-
rubocop-minitest (0.
|
50
|
+
rubocop-minitest (0.26.1)
|
54
51
|
rubocop (>= 0.90, < 2.0)
|
55
52
|
rubocop-rake (0.6.0)
|
56
53
|
rubocop (~> 1.0)
|
57
54
|
ruby-progressbar (1.11.0)
|
58
|
-
|
59
|
-
tzinfo (2.0.4)
|
55
|
+
tzinfo (2.0.5)
|
60
56
|
concurrent-ruby (~> 1.0)
|
61
|
-
unicode-display_width (2.
|
57
|
+
unicode-display_width (2.4.2)
|
62
58
|
|
63
59
|
PLATFORMS
|
64
60
|
arm64-darwin-21
|
65
61
|
x86_64-linux
|
66
62
|
|
67
63
|
DEPENDENCIES
|
68
|
-
|
64
|
+
activejob (>= 6.1, < 8.0)
|
69
65
|
haku!
|
70
66
|
minitest (~> 5.0)
|
71
67
|
pry (~> 0.14)
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,13 @@
|
|
2
2
|
|
3
3
|
![CI](https://github.com/javierav/haku/workflows/CI/badge.svg)
|
4
4
|
|
5
|
-
A library for build simple service objects.
|
5
|
+
A simple library for build simple service objects.
|
6
|
+
|
7
|
+
|
8
|
+
## Status
|
9
|
+
|
10
|
+
> :warning: **This project is still experimental, use with caution!**
|
11
|
+
|
6
12
|
|
7
13
|
## Installation
|
8
14
|
|
@@ -18,11 +24,22 @@ And then execute:
|
|
18
24
|
bundle install
|
19
25
|
```
|
20
26
|
|
27
|
+
|
21
28
|
## Usage
|
22
29
|
|
23
|
-
|
30
|
+
**Haku** is made up of four modules that add functionality to our service objects:
|
31
|
+
|
32
|
+
* [Haku::Core](#hakucore)
|
33
|
+
* [Haku::Delayable](#hakudelayable)
|
34
|
+
* [Haku::Eventable](#hakueventable)
|
35
|
+
* [Haku::Resourceable](#hakuresourceable)
|
36
|
+
|
37
|
+
Additionally, it's available the [Haku::Controller](#hakucontroller) module for use in ours Rails controllers.
|
38
|
+
|
39
|
+
|
40
|
+
### Haku::Core
|
24
41
|
|
25
|
-
|
42
|
+
```ruby
|
26
43
|
class Users::Update
|
27
44
|
include Haku::Core
|
28
45
|
|
@@ -49,95 +66,96 @@ response = Users::Update.call(user: User.first, attributes: { name: "Javier" })
|
|
49
66
|
response.success? # => true
|
50
67
|
response.result # => { resource: <User id="1" ...> }
|
51
68
|
response.resource # => <User id="1" ...>
|
52
|
-
|
69
|
+
```
|
53
70
|
|
54
|
-
|
71
|
+
As you can see, if the payload passed to `success!` or `failure!` is a hash, each key of the hash can be accessed
|
72
|
+
directly in response object.
|
55
73
|
|
56
|
-
|
57
|
-
|
74
|
+
|
75
|
+
### Haku::Delayable
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
class Users::ComputeHours
|
58
79
|
include Haku::Core
|
59
|
-
include Haku::
|
80
|
+
include Haku::Delayable
|
60
81
|
|
61
|
-
input :user
|
62
|
-
on_success :send_email
|
82
|
+
input :user
|
63
83
|
|
64
84
|
def call
|
65
|
-
|
85
|
+
# compute expensive data for user
|
66
86
|
end
|
87
|
+
end
|
67
88
|
|
68
|
-
|
89
|
+
Users::ComputeHours.delayed.call(user: User.first)
|
90
|
+
```
|
69
91
|
|
70
|
-
|
71
|
-
UserMailer.with(user: user).update.deliver_later
|
72
|
-
end
|
73
|
-
end
|
74
|
-
````
|
92
|
+
Use `delayed.call` instead of `call` for execute service object in background using `ActiveJob` job.
|
75
93
|
|
76
|
-
|
94
|
+
#### Customize job
|
77
95
|
|
78
96
|
```ruby
|
79
|
-
|
80
|
-
|
97
|
+
Users::ComputeHours.delayed(job: OtherJob).call(user: User.first)
|
98
|
+
```
|
81
99
|
|
82
|
-
|
83
|
-
execute Users::Update, user: @user, attributes: update_params
|
100
|
+
#### Customize job options
|
84
101
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
render :edit, errors: execution.errors
|
89
|
-
end
|
90
|
-
end
|
102
|
+
```ruby
|
103
|
+
Users::ComputeHours.delayed(job: OtherJob, queue: :low, priority: 2).call(user: User.first)
|
104
|
+
```
|
91
105
|
|
92
|
-
|
106
|
+
You can pass the same options allowed by ActiveJob
|
107
|
+
[set](https://api.rubyonrails.org/v7.0.4/classes/ActiveJob/Core/ClassMethods.html#method-i-set) method:
|
93
108
|
|
94
|
-
|
95
|
-
@user = User.find(params[:id])
|
96
|
-
end
|
109
|
+
#### Config options
|
97
110
|
|
98
|
-
|
99
|
-
|
100
|
-
|
111
|
+
```ruby
|
112
|
+
# config/initializers/haku.rb
|
113
|
+
|
114
|
+
Haku.configure do |config|
|
115
|
+
config.job_queue = "low_priority"
|
101
116
|
end
|
102
117
|
```
|
103
118
|
|
104
|
-
|
119
|
+
| Config | Description | Default value |
|
120
|
+
|:------------|:---------------------------------|:--------------|
|
121
|
+
| `job_queue` | String or Symbol with queue name | `default` |
|
122
|
+
|
105
123
|
|
106
|
-
|
124
|
+
### Haku::Eventable
|
125
|
+
|
126
|
+
```ruby
|
107
127
|
class Users::Update
|
108
128
|
include Haku::Core
|
109
|
-
include Haku::Resourceable
|
110
129
|
include Haku::Eventable
|
111
130
|
|
112
131
|
input :user, :attributes
|
113
132
|
event resource: :user
|
114
133
|
|
115
134
|
def call
|
116
|
-
|
135
|
+
success! resource: user
|
117
136
|
end
|
118
137
|
end
|
119
138
|
|
120
139
|
Users::Update.call(user: User.first, attributes: { name: "Javier" })
|
121
140
|
|
122
141
|
# => call Event.create(name: "user:update", resource: User.first)
|
123
|
-
|
142
|
+
```
|
124
143
|
|
125
|
-
|
144
|
+
The `name` attribute are calculated using the custom proc from `event_name` config option. You can change it with
|
126
145
|
|
127
146
|
```ruby
|
128
|
-
|
129
|
-
include Haku::Core
|
130
|
-
include Haku::Resourceable
|
131
|
-
include Haku::Eventable
|
132
|
-
end
|
133
|
-
|
134
|
-
class Users::Update < ApplicationAction
|
135
|
-
end
|
147
|
+
event name: "custom:name", resource: :user
|
136
148
|
```
|
137
149
|
|
138
|
-
|
150
|
+
#### Properties passed to event model
|
151
|
+
|
152
|
+
For each property passed as payload of `event` class method, it will try to:
|
139
153
|
|
140
|
-
|
154
|
+
1. If is a block, it is called to get the value of property.
|
155
|
+
2. If is a symbol, a method is used to get the value of property:
|
156
|
+
3. In other case, uses the raw value.
|
157
|
+
|
158
|
+
#### Config options
|
141
159
|
|
142
160
|
```ruby
|
143
161
|
# config/initializers/haku.rb
|
@@ -147,24 +165,39 @@ Haku.configure do |config|
|
|
147
165
|
end
|
148
166
|
```
|
149
167
|
|
150
|
-
|
151
|
-
|
152
|
-
|
|
153
|
-
|
154
|
-
| `
|
155
|
-
| `enable_in_action_controller_api` | Include controller helpers in `ActionController::API` | `true` |
|
156
|
-
| `event_model` | Name of the model used for create events | `Event` |
|
157
|
-
| `event_properties` | List of attributes passed from service to event model | `%i[actor resource target context]` |
|
158
|
-
| `event_property_for_name` | Property used for name in event model | `:name` |
|
159
|
-
| `event_name` | String or Proc to determine the event name | Custom Proc. Example: `user:created` for `Users::Create` |
|
168
|
+
| Config | Description | Default value |
|
169
|
+
|:--------------------------|:------------------------------------------------------|:-------------------------------------------------------|
|
170
|
+
| `event_model` | Name of the model used for create events | `Event` |
|
171
|
+
| `event_property_for_name` | Property used for name in event model | `:name` |
|
172
|
+
| `event_name` | String or Proc to determine the event name | Custom Proc. Returns `user:create` for `Users::Create` |
|
160
173
|
|
161
174
|
|
162
|
-
|
175
|
+
### Haku::Resourceable
|
163
176
|
|
164
177
|
This module include helpers to works with *ActiveRecord* compatible model resources, invoking `success!` or `failure!`
|
165
178
|
based in the result of the performed operation.
|
166
179
|
|
167
|
-
|
180
|
+
```ruby
|
181
|
+
class Users::Update
|
182
|
+
include Haku::Core
|
183
|
+
include Haku::Resourceable
|
184
|
+
|
185
|
+
input :user, :attributes
|
186
|
+
on_success :send_email
|
187
|
+
|
188
|
+
def call
|
189
|
+
update_resource(user, attributes)
|
190
|
+
end
|
191
|
+
|
192
|
+
private
|
193
|
+
|
194
|
+
def send_email
|
195
|
+
UserMailer.with(user: user).update.deliver_later
|
196
|
+
end
|
197
|
+
end
|
198
|
+
```
|
199
|
+
|
200
|
+
#### create_resource
|
168
201
|
|
169
202
|
Call to `create` or `<singleton>_create` method of the `parent` object passing the `attributes` and storing
|
170
203
|
the result object in the `ivar` instance variable. Invoke `success!` if the model is persisted or `failure!` in other
|
@@ -177,13 +210,13 @@ case.
|
|
177
210
|
| `ivar` | `Symbol` | Name of the instance variable used to access to the new resource |
|
178
211
|
| `options` | `Hash` | Options hash |
|
179
212
|
|
180
|
-
|
213
|
+
##### options
|
181
214
|
|
182
215
|
| parameter | type | description |
|
183
216
|
|-------------|----------|----------------------------------------------------------------------|
|
184
217
|
| `singleton` | `Symbol` | If the resource should be created using `<singleton>_create` suffix. |
|
185
218
|
|
186
|
-
|
219
|
+
#### update_resource
|
187
220
|
|
188
221
|
Call to `update` method of the `resource` object passing `attributes`to it. Invoke `success!` if the model is updated or
|
189
222
|
`failure!` in other case.
|
@@ -193,7 +226,7 @@ Call to `update` method of the `resource` object passing `attributes`to it. Invo
|
|
193
226
|
| `resource` | `Object` | Resource to be updated |
|
194
227
|
| `attributes` | `Hash` | Attributes to update |
|
195
228
|
|
196
|
-
|
229
|
+
#### destroy_resource
|
197
230
|
|
198
231
|
Call to `destroy` method of the `resource`. Invoke `success!` if the model is destroyed or `failure!` in other case.
|
199
232
|
|
@@ -201,7 +234,7 @@ Call to `destroy` method of the `resource`. Invoke `success!` if the model is de
|
|
201
234
|
|--------------|----------|--------------------------|
|
202
235
|
| `resource` | `Object` | Resource to be destroyed |
|
203
236
|
|
204
|
-
|
237
|
+
#### persist_resource
|
205
238
|
|
206
239
|
| parameter | type | description |
|
207
240
|
|----------------|----------|---------------------------------------------|
|
@@ -210,6 +243,52 @@ Call to `destroy` method of the `resource`. Invoke `success!` if the model is de
|
|
210
243
|
|
211
244
|
For more info please view the [source code](lib/haku/resourceable.rb) of the module.
|
212
245
|
|
246
|
+
|
247
|
+
### Haku::Controller
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
class UsersController < ApplicationController
|
251
|
+
include Haku::Controller
|
252
|
+
|
253
|
+
before_action :find_user
|
254
|
+
|
255
|
+
def update
|
256
|
+
execute Users::Update, user: @user, attributes: update_params
|
257
|
+
|
258
|
+
if execution.success?
|
259
|
+
redirect_to user_path(execution.resource)
|
260
|
+
else
|
261
|
+
render :edit, errors: execution.errors
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
private
|
266
|
+
|
267
|
+
def find_user
|
268
|
+
@user = User.find(params[:id])
|
269
|
+
end
|
270
|
+
|
271
|
+
def update_params
|
272
|
+
params.require(:user).permit(:first_name, :last_name)
|
273
|
+
end
|
274
|
+
end
|
275
|
+
```
|
276
|
+
|
277
|
+
|
278
|
+
### Using parent class
|
279
|
+
|
280
|
+
```ruby
|
281
|
+
class ApplicationAction
|
282
|
+
include Haku::Core
|
283
|
+
include Haku::Resourceable
|
284
|
+
include Haku::Eventable
|
285
|
+
end
|
286
|
+
|
287
|
+
class Users::Update < ApplicationAction
|
288
|
+
end
|
289
|
+
```
|
290
|
+
|
291
|
+
|
213
292
|
## Development
|
214
293
|
|
215
294
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
|
@@ -221,16 +300,19 @@ To release a new version, update the version number in `version.rb`, and then ru
|
|
221
300
|
which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to
|
222
301
|
[rubygems.org](https://rubygems.org).
|
223
302
|
|
303
|
+
|
224
304
|
## Contributing
|
225
305
|
|
226
306
|
Bug reports and pull requests are welcome, please follow
|
227
307
|
[Github Flow](https://docs.github.com/en/get-started/quickstart/github-flow).
|
228
308
|
|
309
|
+
|
229
310
|
## Code of Conduct
|
230
311
|
|
231
312
|
Everyone interacting in the Haku project's codebases, issue trackers, chat rooms and mailing lists is expected to
|
232
313
|
follow the [code of conduct](https://github.com/javierav/haku/blob/development/CODE_OF_CONDUCT.md).
|
233
314
|
|
315
|
+
|
234
316
|
## License
|
235
317
|
|
236
|
-
Copyright © 2022 Javier Aranda. Released under the terms of the [MIT license](LICENSE).
|
318
|
+
Copyright © 2022-2023 Javier Aranda. Released under the terms of the [MIT license](LICENSE).
|
data/lib/haku/core.rb
CHANGED
@@ -14,6 +14,7 @@ module Haku
|
|
14
14
|
attr_reader :params
|
15
15
|
|
16
16
|
class_attribute :haku_inputs, default: []
|
17
|
+
class_attribute :haku_before_call_callbacks, default: []
|
17
18
|
class_attribute :haku_success_callbacks, default: []
|
18
19
|
class_attribute :haku_failure_callbacks, default: []
|
19
20
|
end
|
@@ -35,6 +36,10 @@ module Haku
|
|
35
36
|
self.haku_inputs += names
|
36
37
|
end
|
37
38
|
|
39
|
+
def before_call(*methods)
|
40
|
+
self.haku_before_call_callbacks += methods
|
41
|
+
end
|
42
|
+
|
38
43
|
def on_success(*methods)
|
39
44
|
self.haku_success_callbacks += methods
|
40
45
|
end
|
@@ -48,13 +53,16 @@ module Haku
|
|
48
53
|
|
49
54
|
module Callable
|
50
55
|
def call
|
51
|
-
response = catch(:finish)
|
56
|
+
response = catch(:finish) do
|
57
|
+
(self.class.send(:haku_before_call_callbacks) || []).each { |cb| send(cb) }
|
58
|
+
super
|
59
|
+
end
|
52
60
|
|
53
61
|
status = response.is_a?(Finish) ? response.status : :success
|
54
62
|
payload = response.is_a?(Finish) ? response.payload : response
|
55
63
|
|
56
64
|
Result.new(status, payload).tap do
|
57
|
-
|
65
|
+
haku_run_callbacks(status)
|
58
66
|
end
|
59
67
|
end
|
60
68
|
end
|
@@ -77,7 +85,7 @@ module Haku
|
|
77
85
|
throw :finish, Finish.new(:failure, data)
|
78
86
|
end
|
79
87
|
|
80
|
-
def
|
88
|
+
def haku_run_callbacks(status)
|
81
89
|
(self.class.send("haku_#{status}_callbacks") || []).each { |cb| send(cb) }
|
82
90
|
end
|
83
91
|
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/concern"
|
4
|
+
require "active_support/core_ext/string/inflections"
|
5
|
+
|
6
|
+
module Haku
|
7
|
+
module Delayable
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
if defined?(ActiveJob::Base)
|
11
|
+
class Job < ActiveJob::Base
|
12
|
+
def perform(klass, params)
|
13
|
+
klass.call(params)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Delayed
|
19
|
+
def initialize(service, options={})
|
20
|
+
@service = service
|
21
|
+
@options = options.reverse_merge(
|
22
|
+
job: "::Haku::Delayable::Job",
|
23
|
+
queue: Haku.job_queue
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
def call(params={})
|
28
|
+
if job.present? && defined?(job)
|
29
|
+
job.set(@options).perform_later(@service, params)
|
30
|
+
else
|
31
|
+
@service.call(params)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def job
|
38
|
+
@job ||= begin
|
39
|
+
job = @options.delete(:job)
|
40
|
+
job.is_a?(String) ? job.safe_constantize : job
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module ClassMethods
|
46
|
+
def delayed(options={})
|
47
|
+
::Haku::Delayable::Delayed.new(self, options)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/haku/eventable.rb
CHANGED
@@ -39,15 +39,19 @@ module Haku
|
|
39
39
|
|
40
40
|
def haku_prepare_event_data(evt, data={})
|
41
41
|
data.tap do
|
42
|
-
haku_event_data_base(data)
|
43
42
|
haku_event_data_name(data, evt)
|
44
43
|
haku_event_data_values(data, evt)
|
45
44
|
end
|
46
45
|
end
|
47
46
|
|
48
|
-
def
|
49
|
-
Haku.
|
50
|
-
|
47
|
+
def haku_event_data_name(data, evt)
|
48
|
+
key = Haku.event_property_for_name.to_sym
|
49
|
+
data[key] = haku_process_value(evt[key] || Haku.event_name)
|
50
|
+
end
|
51
|
+
|
52
|
+
def haku_event_data_values(data, evt)
|
53
|
+
evt.except(Haku.event_property_for_name.to_sym).each_pair do |key, value|
|
54
|
+
data[key] = haku_process_value(value)
|
51
55
|
end
|
52
56
|
end
|
53
57
|
|
@@ -59,17 +63,6 @@ module Haku
|
|
59
63
|
end
|
60
64
|
end
|
61
65
|
|
62
|
-
def haku_event_data_name(data, evt)
|
63
|
-
key = Haku.event_property_for_name.to_sym
|
64
|
-
data[key] = evt[key] || haku_process_value(Haku.event_name)
|
65
|
-
end
|
66
|
-
|
67
|
-
def haku_event_data_values(data, evt)
|
68
|
-
evt.except(Haku.event_property_for_name.to_sym).each_pair do |key, value|
|
69
|
-
data[key] = haku_process_value(value)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
66
|
def haku_create_event(data)
|
74
67
|
Haku.event_model.safe_constantize&.create(data)
|
75
68
|
end
|
data/lib/haku/version.rb
CHANGED
data/lib/haku.rb
CHANGED
@@ -4,18 +4,19 @@ require "active_support/core_ext/module/attribute_accessors"
|
|
4
4
|
require "active_support/core_ext/string/inflections"
|
5
5
|
require_relative "haku/controller"
|
6
6
|
require_relative "haku/core"
|
7
|
+
require_relative "haku/delayable"
|
7
8
|
require_relative "haku/eventable"
|
8
9
|
require_relative "haku/resourceable"
|
9
10
|
require_relative "haku/version"
|
10
11
|
|
11
12
|
module Haku
|
12
13
|
mattr_accessor :event_model, default: "Event"
|
13
|
-
mattr_accessor :event_properties, default: %i[actor resource target context]
|
14
14
|
mattr_accessor :event_property_for_name, default: :name
|
15
15
|
mattr_accessor :event_name, default: proc {
|
16
16
|
chain = self.class.name.underscore.split("/")
|
17
17
|
(chain[0...-1].map(&:singularize) + [chain.last]).join(":")
|
18
18
|
}
|
19
|
+
mattr_accessor :job_queue, default: :default
|
19
20
|
|
20
21
|
class << self
|
21
22
|
def configure
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: haku
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Javier Aranda
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,7 +16,7 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '6.
|
19
|
+
version: '6.1'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
22
|
version: '8.0'
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '6.
|
29
|
+
version: '6.1'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '8.0'
|
@@ -44,7 +44,6 @@ files:
|
|
44
44
|
- ".pryrc"
|
45
45
|
- ".rubocop.yml"
|
46
46
|
- ".tool-versions"
|
47
|
-
- Appraisals
|
48
47
|
- CHANGELOG.md
|
49
48
|
- CODE_OF_CONDUCT.md
|
50
49
|
- Gemfile
|
@@ -52,12 +51,10 @@ files:
|
|
52
51
|
- LICENSE
|
53
52
|
- README.md
|
54
53
|
- Rakefile
|
55
|
-
- gemfiles/activesupport_6.0.gemfile
|
56
|
-
- gemfiles/activesupport_6.1.gemfile
|
57
|
-
- gemfiles/activesupport_7.0.gemfile
|
58
54
|
- lib/haku.rb
|
59
55
|
- lib/haku/controller.rb
|
60
56
|
- lib/haku/core.rb
|
57
|
+
- lib/haku/delayable.rb
|
61
58
|
- lib/haku/eventable.rb
|
62
59
|
- lib/haku/resourceable.rb
|
63
60
|
- lib/haku/result.rb
|
@@ -67,8 +64,8 @@ licenses:
|
|
67
64
|
- MIT
|
68
65
|
metadata:
|
69
66
|
homepage_uri: https://github.com/javierav/haku
|
70
|
-
source_code_uri: https://github.com/javierav/haku/tree/v1.
|
71
|
-
changelog_uri: https://github.com/javierav/haku/blob/v1.
|
67
|
+
source_code_uri: https://github.com/javierav/haku/tree/v1.5.0
|
68
|
+
changelog_uri: https://github.com/javierav/haku/blob/v1.5.0/CHANGELOG.md
|
72
69
|
rubygems_mfa_required: 'true'
|
73
70
|
post_install_message:
|
74
71
|
rdoc_options:
|
@@ -79,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
76
|
requirements:
|
80
77
|
- - ">="
|
81
78
|
- !ruby/object:Gem::Version
|
82
|
-
version: 2.
|
79
|
+
version: 2.7.0
|
83
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
81
|
requirements:
|
85
82
|
- - ">="
|
@@ -89,5 +86,5 @@ requirements: []
|
|
89
86
|
rubygems_version: 3.3.7
|
90
87
|
signing_key:
|
91
88
|
specification_version: 4
|
92
|
-
summary: A library for build simple service objects
|
89
|
+
summary: A simple library for build simple service objects
|
93
90
|
test_files: []
|
data/Appraisals
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "appraisal", "~> 2.4"
|
6
|
-
gem "minitest", "~> 5.0"
|
7
|
-
gem "pry", "~> 0.14"
|
8
|
-
gem "rake", "~> 13.0"
|
9
|
-
gem "rubocop", "~> 1.21"
|
10
|
-
gem "rubocop-minitest", "~> 0.19"
|
11
|
-
gem "rubocop-rake", "~> 0.6"
|
12
|
-
gem "activesupport", "6.0"
|
13
|
-
|
14
|
-
gemspec path: "../"
|
@@ -1,14 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "appraisal", "~> 2.4"
|
6
|
-
gem "minitest", "~> 5.0"
|
7
|
-
gem "pry", "~> 0.14"
|
8
|
-
gem "rake", "~> 13.0"
|
9
|
-
gem "rubocop", "~> 1.21"
|
10
|
-
gem "rubocop-minitest", "~> 0.19"
|
11
|
-
gem "rubocop-rake", "~> 0.6"
|
12
|
-
gem "activesupport", "6.1"
|
13
|
-
|
14
|
-
gemspec path: "../"
|
@@ -1,14 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "appraisal", "~> 2.4"
|
6
|
-
gem "minitest", "~> 5.0"
|
7
|
-
gem "pry", "~> 0.14"
|
8
|
-
gem "rake", "~> 13.0"
|
9
|
-
gem "rubocop", "~> 1.21"
|
10
|
-
gem "rubocop-minitest", "~> 0.19"
|
11
|
-
gem "rubocop-rake", "~> 0.6"
|
12
|
-
gem "activesupport", "7.0"
|
13
|
-
|
14
|
-
gemspec path: "../"
|