fluxo 0.2.0 → 0.2.1
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/Gemfile.lock +1 -1
- data/README.md +51 -0
- data/ci/Gemfile.activemodel-4.lock +1 -1
- data/ci/Gemfile.activemodel-5.lock +1 -1
- data/ci/Gemfile.activemodel-6.lock +1 -1
- data/ci/Gemfile.activemodel-7.lock +1 -1
- data/lib/fluxo/operation.rb +3 -3
- data/lib/fluxo/result.rb +10 -2
- data/lib/fluxo/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 383f612dadbf2c9b9c7cd5d8e4e09e3f140a8665405e3069e29e1c6629ff1da1
|
4
|
+
data.tar.gz: d91c37a9e158ad85b039cd3fb82d09881ed8390083e60ace2f3c345549902cff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7ba58999435d17f261decb8bd64a37f2f75135027a3e3683cce67ceae08bd065145c31e0c5e9f70c394fa944faa2e20986cf15860ca90236c0feb06d54a68ab
|
7
|
+
data.tar.gz: 8d2b54df3b612c2c6f20430d9086ff2a53a958ae9989a8605c05bfe12ebf2e3c93978b1add2af3919f5a6a99c294ebe5b610ab9b10f34f21ba7979bdb5e30406
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -240,6 +240,57 @@ class CreateUserOperation < Fluxo::Operation(:name, :email)
|
|
240
240
|
end
|
241
241
|
```
|
242
242
|
|
243
|
+
### Operation Validation
|
244
|
+
|
245
|
+
If you have the `ActiveModel` gem installed, you can use the `validations` method to define validations on the operation.
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
class SubscribeOperation < Fluxo::Operation(:name, :email)
|
249
|
+
validations do
|
250
|
+
validates :name, presence: true
|
251
|
+
validates :email, presence: true, format: { with: /\A[^@]+@[^@]+\z/ }
|
252
|
+
end
|
253
|
+
|
254
|
+
def call!(name:, email:)
|
255
|
+
# ...
|
256
|
+
end
|
257
|
+
end
|
258
|
+
```
|
259
|
+
|
260
|
+
### Operations Composition
|
261
|
+
|
262
|
+
To promote single responsibility principle, Fluxo allows compose a complex operation flow by combining other operations.
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
class DoubleOperation < Fluxo::Operation(:num)
|
266
|
+
def call!(num:)
|
267
|
+
Success(num: num * 2)
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
class SquareOperation < Fluxo::Operation(:num)
|
272
|
+
def call!(num:)
|
273
|
+
Success(num: num * 2)
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
class ArithmeticOperation < Fluxo::Operation(:num)
|
278
|
+
flow :normalize, :double, :square
|
279
|
+
|
280
|
+
def normalize(num:)
|
281
|
+
Success(num: num.to_i)
|
282
|
+
end
|
283
|
+
|
284
|
+
def double(num:)
|
285
|
+
DoubleOperation.call(num: num)
|
286
|
+
end
|
287
|
+
|
288
|
+
def square(num:)
|
289
|
+
SquareOperation.call(num: num)
|
290
|
+
end
|
291
|
+
end
|
292
|
+
```
|
293
|
+
|
243
294
|
### Configuration
|
244
295
|
|
245
296
|
```ruby
|
data/lib/fluxo/operation.rb
CHANGED
@@ -42,7 +42,7 @@ module Fluxo
|
|
42
42
|
# Calls step-method by step-method always passing the value to the next step
|
43
43
|
# If one of the methods is a failure stop the execution and return a result.
|
44
44
|
def __execute_flow__(steps: [], attributes: {})
|
45
|
-
transient_attributes, transient_ids = attributes.dup, {
|
45
|
+
transient_attributes, transient_ids = attributes.dup, Hash.new { |h, k| h[k] = [] }
|
46
46
|
__validate_attributes__(first_step: steps.first, attributes: transient_attributes)
|
47
47
|
|
48
48
|
result = nil
|
@@ -57,7 +57,7 @@ module Fluxo
|
|
57
57
|
end
|
58
58
|
else
|
59
59
|
result = __wrap_result__(send(step, **transient_attributes))
|
60
|
-
transient_ids
|
60
|
+
transient_ids[result.type].push(*result.ids)
|
61
61
|
end
|
62
62
|
|
63
63
|
break unless result.success?
|
@@ -70,7 +70,7 @@ module Fluxo
|
|
70
70
|
)
|
71
71
|
end
|
72
72
|
end
|
73
|
-
result.
|
73
|
+
result.mutate(ids: transient_ids[result.type].uniq, operation: self)
|
74
74
|
end
|
75
75
|
|
76
76
|
# @param value_or_result_id [Any] The value for the result or the id when the result comes from block
|
data/lib/fluxo/result.rb
CHANGED
@@ -2,8 +2,7 @@
|
|
2
2
|
|
3
3
|
module Fluxo
|
4
4
|
class Result
|
5
|
-
attr_reader :operation, :type, :value
|
6
|
-
attr_accessor :ids
|
5
|
+
attr_reader :operation, :type, :value, :ids
|
7
6
|
|
8
7
|
# @param options [Hash]
|
9
8
|
# @option options [Fluxo::Operation] :operation The operation instance that gererated this result
|
@@ -17,6 +16,15 @@ module Fluxo
|
|
17
16
|
@ids = Array(ids)
|
18
17
|
end
|
19
18
|
|
19
|
+
def mutate(**attrs)
|
20
|
+
self.class.new(**{operation: operation, type: type, value: value, ids: ids}.merge(attrs))
|
21
|
+
end
|
22
|
+
|
23
|
+
def ==(other)
|
24
|
+
other.is_a?(self.class) && other.operation == operation && other.type == type && other.value == value && other.ids == ids
|
25
|
+
end
|
26
|
+
alias_method :eql?, :==
|
27
|
+
|
20
28
|
# @return [Boolean] true if the result is a success
|
21
29
|
def success?
|
22
30
|
type == :ok
|
data/lib/fluxo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fluxo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcos G. Zimmermann
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-01-
|
11
|
+
date: 2022-01-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: standard
|