fluxo 0.2.0 → 0.2.1

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: 9453729318ec0b9d59e2833d19a4383c4b2f1bb241a8f97774c3f3b2cb4e6654
4
- data.tar.gz: b428e33f29ababb0e2099eb9807f295fb899c965088ab2e650c111961d24c76a
3
+ metadata.gz: 383f612dadbf2c9b9c7cd5d8e4e09e3f140a8665405e3069e29e1c6629ff1da1
4
+ data.tar.gz: d91c37a9e158ad85b039cd3fb82d09881ed8390083e60ace2f3c345549902cff
5
5
  SHA512:
6
- metadata.gz: 6ce303be1b5fa233fb9fc0e72eccebafd6d36e5ac81b2f6b8b1796c07c63bc5cbc097ed5fa7f48fdaf3457d3414335b8bbd1a63765557e7f3de0279a0dbb78ba
7
- data.tar.gz: 514da4e79141fae0847ef16e2f4e9c67ac0246de87415277c657c93471cfe444a6eca8ec332b7a64aaa6286b61375b80d46ce49185fb5942b887d5a01ea4a271
6
+ metadata.gz: b7ba58999435d17f261decb8bd64a37f2f75135027a3e3683cce67ceae08bd065145c31e0c5e9f70c394fa944faa2e20986cf15860ca90236c0feb06d54a68ab
7
+ data.tar.gz: 8d2b54df3b612c2c6f20430d9086ff2a53a958ae9989a8605c05bfe12ebf2e3c93978b1add2af3919f5a6a99c294ebe5b610ab9b10f34f21ba7979bdb5e30406
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fluxo (0.2.0)
4
+ fluxo (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- fluxo (0.1.0)
4
+ fluxo (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- fluxo (0.1.0)
4
+ fluxo (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- fluxo (0.1.0)
4
+ fluxo (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- fluxo (0.1.0)
4
+ fluxo (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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, {ok: [], failure: [], exception: []}
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.fetch(result.type).push(*result.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.tap { |r| r.ids = transient_ids.fetch(r.type).uniq }
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fluxo
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.1"
5
5
  end
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.0
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-17 00:00:00.000000000 Z
11
+ date: 2022-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: standard