haku 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca30959f8f3163c1aa8c7179f0e4ff3d84b2c46b736733ddefdbc3071c433b08
4
- data.tar.gz: 8f1aaff1f0b2c64480ca5310b42cd68892306b64ac366d7813e99cf2f2616fce
3
+ metadata.gz: 137f060c137bed529cdd82679e0880674ed6a6b5d62a9865bf5f52fb8c89c805
4
+ data.tar.gz: 8ca69778bad99f1cd7f2ea6a64fdc0784337741acbde926ff487f5bc2312ff67
5
5
  SHA512:
6
- metadata.gz: 89b65efe9dc09eff84a3cf25def2301afaab54c296f8e0eb4b620750addc71af6f222aace23774edfd4b737fd88aed36fb2511373ed7fb3116eb811e97736f27
7
- data.tar.gz: 296982f10418908126079b27030d5061316703cf2dbe415b8e707414954fd08d6ac36f93a236d930f7c25f818fffbfc3e475b8548f7aefb922b6dd98dcb79c9d
6
+ metadata.gz: 53a9a2ffdd2f719e2aad8878130a7a2359cd818947be00cedaf82477262cafab6431e357520fb14a738f9fbc80b790a42077d9a68f000c3fa769ffea45369c04
7
+ data.tar.gz: 56ec2ff6a811220f409ed65df974493ffb0adcb77b7eb50f9a114e63a3c79af4160728b66278126d78886892f6e35bb63d973a77d26895bca7f3612a1fbc4efc
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.5.0] - 2023-01-25
4
+ * **Breaking**: Simplify Haku::Eventable module
5
+
3
6
  ## [1.4.0] - 2022-09-19
4
7
  - Improve `Haku::Eventable` module to support more data sources for event properties
5
8
  - Add `Haku::Delayable` module to execute service object in background
data/Gemfile CHANGED
@@ -4,7 +4,7 @@ source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
6
 
7
- gem "appraisal", github: "excid3/appraisal", branch: "fix-bundle-env"
7
+ gem "activejob", ">= 6.1", "< 8.0"
8
8
  gem "minitest", "~> 5.0"
9
9
  gem "pry", "~> 0.14"
10
10
  gem "rake", "~> 13.0"
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.4.0)
4
+ haku (1.5.0)
15
5
  activesupport (>= 6.1, < 8.0)
16
6
 
17
7
  GEM
18
8
  remote: https://rubygems.org/
19
9
  specs:
20
- activesupport (7.0.4)
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.1.10)
20
+ concurrent-ruby (1.2.0)
21
+ globalid (1.0.1)
22
+ activesupport (>= 5.0)
28
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.15.0)
27
+ minitest (5.17.0)
32
28
  parallel (1.22.1)
33
- parser (3.1.1.0)
29
+ parser (3.2.0.0)
34
30
  ast (~> 2.4.1)
35
- pry (0.14.1)
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.1)
36
+ regexp_parser (2.6.2)
41
37
  rexml (3.2.5)
42
- rubocop (1.26.1)
38
+ rubocop (1.44.1)
39
+ json (~> 2.3)
43
40
  parallel (~> 1.10)
44
- parser (>= 3.1.0.0)
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.16.0, < 2.0)
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 (>= 1.4.0, < 3.0)
51
- rubocop-ast (1.16.0)
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.19.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
- thor (1.2.1)
59
55
  tzinfo (2.0.5)
60
56
  concurrent-ruby (~> 1.0)
61
- unicode-display_width (2.1.0)
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
- appraisal!
64
+ activejob (>= 6.1, < 8.0)
69
65
  haku!
70
66
  minitest (~> 5.0)
71
67
  pry (~> 0.14)
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2022 Javier Aranda
3
+ Copyright (c) 2022-2023 Javier Aranda
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
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,16 +24,18 @@ 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:
24
31
 
25
- * `Haku::Core`
26
- * `Haku::Delayable`
27
- * `Haku::Eventable`
28
- * `Haku::Resourceable`
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.
29
38
 
30
- Additionally, it's available the `Haku::Controller` module for use in ours Rails controllers.
31
39
 
32
40
  ### Haku::Core
33
41
 
@@ -63,9 +71,8 @@ response.resource # => <User id="1" ...>
63
71
  As you can see, if the payload passed to `success!` or `failure!` is a hash, each key of the hash can be accessed
64
72
  directly in response object.
65
73
 
66
- ### Haku::Delayable
67
74
 
68
- #### Basic example
75
+ ### Haku::Delayable
69
76
 
70
77
  ```ruby
71
78
  class Users::ComputeHours
@@ -99,10 +106,22 @@ Users::ComputeHours.delayed(job: OtherJob, queue: :low, priority: 2).call(user:
99
106
  You can pass the same options allowed by ActiveJob
100
107
  [set](https://api.rubyonrails.org/v7.0.4/classes/ActiveJob/Core/ClassMethods.html#method-i-set) method:
101
108
 
109
+ #### Config options
102
110
 
103
- ### Haku::Eventable
111
+ ```ruby
112
+ # config/initializers/haku.rb
113
+
114
+ Haku.configure do |config|
115
+ config.job_queue = "low_priority"
116
+ end
117
+ ```
104
118
 
105
- #### Basic example
119
+ | Config | Description | Default value |
120
+ |:------------|:---------------------------------|:--------------|
121
+ | `job_queue` | String or Symbol with queue name | `default` |
122
+
123
+
124
+ ### Haku::Eventable
106
125
 
107
126
  ```ruby
108
127
  class Users::Update
@@ -125,30 +144,39 @@ Users::Update.call(user: User.first, attributes: { name: "Javier" })
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
- event name: 'custom:name', resource: :user
147
+ event name: "custom:name", resource: :user
129
148
  ```
130
149
 
131
150
  #### Properties passed to event model
132
151
 
133
- Properties that should be passed to event model are defined in `event_properties` config option and by
134
- default are `actor` `resource`, `target` and `context`.
152
+ For each property passed as payload of `event` class method, it will try to:
153
+
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
159
+
160
+ ```ruby
161
+ # config/initializers/haku.rb
162
+
163
+ Haku.configure do |config|
164
+ config.event_model = "EventLog"
165
+ end
166
+ ```
135
167
 
136
- 1. Append base properties. For each property defined in `event_properties` config option, it will try to:
137
- 1. If defined a instance variable `@event_<property>`, uses the instance variable value to get the value of property.
138
- 2. If respond_to method called `event_<property>`, method is used to get the value of property.
139
- 3. If defined a instance variable `@<property>`, uses the instance variable value to get the value of property.
140
- 4. If respond_to method called `<property>`, method is used to get the value of property.
141
- 5. In other case, the property is not appended.
142
- 2. Append properties defined in `event` class method. Overwrites previous values. For each property, will try to:
143
- 1. If is a block, it is called to get the value of property.
144
- 2. If is a symbol:
145
- 1. If defined a instance variable `@<property>`, uses the instance variable value to get the value of property.
146
- 2. If respond_to `<property>`, method is used to get the value of property.
147
- 3. In other case, uses the raw value.
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` |
148
173
 
149
174
 
150
175
  ### Haku::Resourceable
151
176
 
177
+ This module include helpers to works with *ActiveRecord* compatible model resources, invoking `success!` or `failure!`
178
+ based in the result of the performed operation.
179
+
152
180
  ```ruby
153
181
  class Users::Update
154
182
  include Haku::Core
@@ -169,6 +197,52 @@ class Users::Update
169
197
  end
170
198
  ```
171
199
 
200
+ #### create_resource
201
+
202
+ Call to `create` or `<singleton>_create` method of the `parent` object passing the `attributes` and storing
203
+ the result object in the `ivar` instance variable. Invoke `success!` if the model is persisted or `failure!` in other
204
+ case.
205
+
206
+ | parameter | type | description |
207
+ |--------------|----------|------------------------------------------------------------------|
208
+ | `parent` | `Object` | Parent object where new resource will be created |
209
+ | `attributes` | `Hash` | Attributes for create |
210
+ | `ivar` | `Symbol` | Name of the instance variable used to access to the new resource |
211
+ | `options` | `Hash` | Options hash |
212
+
213
+ ##### options
214
+
215
+ | parameter | type | description |
216
+ |-------------|----------|----------------------------------------------------------------------|
217
+ | `singleton` | `Symbol` | If the resource should be created using `<singleton>_create` suffix. |
218
+
219
+ #### update_resource
220
+
221
+ Call to `update` method of the `resource` object passing `attributes`to it. Invoke `success!` if the model is updated or
222
+ `failure!` in other case.
223
+
224
+ | parameter | type | description |
225
+ |--------------|----------|------------------------|
226
+ | `resource` | `Object` | Resource to be updated |
227
+ | `attributes` | `Hash` | Attributes to update |
228
+
229
+ #### destroy_resource
230
+
231
+ Call to `destroy` method of the `resource`. Invoke `success!` if the model is destroyed or `failure!` in other case.
232
+
233
+ | parameter | type | description |
234
+ |--------------|----------|--------------------------|
235
+ | `resource` | `Object` | Resource to be destroyed |
236
+
237
+ #### persist_resource
238
+
239
+ | parameter | type | description |
240
+ |----------------|----------|---------------------------------------------|
241
+ | `resource` | `Object` | Resource to be destroyed |
242
+ | `save_options` | `Hash` | Options passed to `save` method of resource |
243
+
244
+ For more info please view the [source code](lib/haku/resourceable.rb) of the module.
245
+
172
246
 
173
247
  ### Haku::Controller
174
248
 
@@ -215,81 +289,6 @@ end
215
289
  ```
216
290
 
217
291
 
218
- ## Configure
219
-
220
- ### Example
221
-
222
- ```ruby
223
- # config/initializers/haku.rb
224
-
225
- Haku.configure do |config|
226
- config.event_model = "EventLog"
227
- end
228
- ```
229
-
230
- ### Allowed options
231
-
232
- | Config | Description | Default value |
233
- |:--------------------------|:------------------------------------------------------|:--------------------------------------------------------|
234
- | `event_model` | Name of the model used for create events | `Event` |
235
- | `event_properties` | List of attributes passed from service to event model | `%i[actor resource target context]` |
236
- | `event_property_for_name` | Property used for name in event model | `:name` |
237
- | `event_name` | String or Proc to determine the event name | Custom Proc. Example: `user:create` for `Users::Create` |
238
- | `job_queue` | String or Symbol with queue name | `default` |
239
-
240
-
241
- ## Resourceable
242
-
243
- This module include helpers to works with *ActiveRecord* compatible model resources, invoking `success!` or `failure!`
244
- based in the result of the performed operation.
245
-
246
- ### create_resource
247
-
248
- Call to `create` or `<singleton>_create` method of the `parent` object passing the `attributes` and storing
249
- the result object in the `ivar` instance variable. Invoke `success!` if the model is persisted or `failure!` in other
250
- case.
251
-
252
- | parameter | type | description |
253
- |--------------|----------|------------------------------------------------------------------|
254
- | `parent` | `Object` | Parent object where new resource will be created |
255
- | `attributes` | `Hash` | Attributes for create |
256
- | `ivar` | `Symbol` | Name of the instance variable used to access to the new resource |
257
- | `options` | `Hash` | Options hash |
258
-
259
- #### options
260
-
261
- | parameter | type | description |
262
- |-------------|----------|----------------------------------------------------------------------|
263
- | `singleton` | `Symbol` | If the resource should be created using `<singleton>_create` suffix. |
264
-
265
- ### update_resource
266
-
267
- Call to `update` method of the `resource` object passing `attributes`to it. Invoke `success!` if the model is updated or
268
- `failure!` in other case.
269
-
270
- | parameter | type | description |
271
- |--------------|----------|------------------------|
272
- | `resource` | `Object` | Resource to be updated |
273
- | `attributes` | `Hash` | Attributes to update |
274
-
275
- ### destroy_resource
276
-
277
- Call to `destroy` method of the `resource`. Invoke `success!` if the model is destroyed or `failure!` in other case.
278
-
279
- | parameter | type | description |
280
- |--------------|----------|--------------------------|
281
- | `resource` | `Object` | Resource to be destroyed |
282
-
283
- ### persist_resource
284
-
285
- | parameter | type | description |
286
- |----------------|----------|---------------------------------------------|
287
- | `resource` | `Object` | Resource to be destroyed |
288
- | `save_options` | `Hash` | Options passed to `save` method of resource |
289
-
290
- For more info please view the [source code](lib/haku/resourceable.rb) of the module.
291
-
292
-
293
292
  ## Development
294
293
 
295
294
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
@@ -316,4 +315,4 @@ follow the [code of conduct](https://github.com/javierav/haku/blob/development/C
316
315
 
317
316
  ## License
318
317
 
319
- 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).
@@ -39,30 +39,11 @@ 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 haku_event_data_base(data)
49
- Haku.event_properties.each do |property|
50
- haku_event_data_base_value_for_property(data, property)
51
- end
52
- end
53
-
54
- def haku_event_data_base_value_for_property(data, property)
55
- if instance_variable_defined?("@event_#{property}")
56
- data[property] = instance_variable_get("@event_#{property}")
57
- elsif respond_to?("event_#{property}", true)
58
- data[property] = send("event_#{property}")
59
- elsif instance_variable_defined?("@#{property}")
60
- data[property] = instance_variable_get("@#{property}")
61
- elsif respond_to?(property, true)
62
- data[property] = send(property)
63
- end
64
- end
65
-
66
47
  def haku_event_data_name(data, evt)
67
48
  key = Haku.event_property_for_name.to_sym
68
49
  data[key] = haku_process_value(evt[key] || Haku.event_name)
@@ -78,14 +59,10 @@ module Haku
78
59
  if value.respond_to?(:call)
79
60
  instance_exec(&value)
80
61
  else
81
- value.is_a?(Symbol) ? haku_process_symbol_value(value) : value
62
+ value.is_a?(Symbol) ? send(value) : value
82
63
  end
83
64
  end
84
65
 
85
- def haku_process_symbol_value(value)
86
- instance_variable_defined?("@#{value}") ? instance_variable_get("@#{value}") : send(value)
87
- end
88
-
89
66
  def haku_create_event(data)
90
67
  Haku.event_model.safe_constantize&.create(data)
91
68
  end
data/lib/haku/version.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  module Haku
4
4
  module VERSION
5
5
  MAJOR = 1
6
- MINOR = 4
6
+ MINOR = 5
7
7
  TINY = 0
8
8
  PRE = nil
9
9
 
data/lib/haku.rb CHANGED
@@ -11,7 +11,6 @@ require_relative "haku/version"
11
11
 
12
12
  module Haku
13
13
  mattr_accessor :event_model, default: "Event"
14
- mattr_accessor :event_properties, default: %i[actor resource target context]
15
14
  mattr_accessor :event_property_for_name, default: :name
16
15
  mattr_accessor :event_name, default: proc {
17
16
  chain = self.class.name.underscore.split("/")
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.0
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: 2022-09-19 00:00:00.000000000 Z
11
+ date: 2023-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -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,8 +51,6 @@ files:
52
51
  - LICENSE
53
52
  - README.md
54
53
  - Rakefile
55
- - gemfiles/rails_6.1.gemfile
56
- - gemfiles/rails_7.0.gemfile
57
54
  - lib/haku.rb
58
55
  - lib/haku/controller.rb
59
56
  - lib/haku/core.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.4.0
71
- changelog_uri: https://github.com/javierav/haku/blob/v1.4.0/CHANGELOG.md
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:
@@ -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,7 +0,0 @@
1
- appraise "rails-6.1" do
2
- gem "rails", "~> 6.1.0"
3
- end
4
-
5
- appraise "rails-7.0" do
6
- gem "rails", "~> 7.0"
7
- end
@@ -1,14 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal", github: "excid3/appraisal", branch: "fix-bundle-env"
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 "rails", "~> 6.1.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", github: "excid3/appraisal", branch: "fix-bundle-env"
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 "rails", "~> 7.0"
13
-
14
- gemspec path: "../"