u-case 2.5.0 → 2.6.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 349609a8d927ab7fd8edb1eb96141dff2f78f9957f31f2b88a8975239fc0de87
4
- data.tar.gz: 4b6293023f49f28dd8283656a0d423f7d24abbfb7f010a90ba4d0dd17f577d22
3
+ metadata.gz: de7b8e442e0b578b478d9d63f05901c9023884f3f2b15a1819228af36fe7afc5
4
+ data.tar.gz: 50e9128500306350a56102b93ca8bd171c75ecae24899c6157a7955e845221a3
5
5
  SHA512:
6
- metadata.gz: 0553e560e40f71d0e8284ebbbbf7a51326d8c9bc9de6ad0de76db526466a94f633e2b038563868a2c93234800b7bdacc30a99ba57f1624ce08463fdc012d165d
7
- data.tar.gz: 2866ca201676827b2ffc82877b1615e7ecbb6fc6e993df17007c6446b348a850266faf162a0953954ee6adfb0d35a88c303bee385d2fb52a9c228f45513655db
6
+ metadata.gz: 93fcec72232ac2d1ce479cdc2efa02d7b4eddf0d3b57e2c93346ecb6359a0f9ccba33f7851b2115c813ef53f79f9276984864f172c341be23be8c11120e933fe
7
+ data.tar.gz: 9993e5cdcbe6f1e864c32a9db7f637c1cdb839bd838d1823e64b2646b704b079d9f83915af65e364abe8edd66034952202909a42833a634d04aeac11eee384bd
data/README.md CHANGED
@@ -32,6 +32,7 @@ The main project goals are:
32
32
  - [Why the failure hook (without a type) exposes a different kind of data?](#why-the-failure-hook-without-a-type-exposes-a-different-kind-of-data)
33
33
  - [What happens if a result hook was declared multiple times?](#what-happens-if-a-result-hook-was-declared-multiple-times)
34
34
  - [How to use the `Micro::Case::Result#then` method?](#how-to-use-the-microcaseresultthen-method)
35
+ - [How to make attributes data injection using this feature?](#how-to-make-attributes-data-injection-using-this-feature)
35
36
  - [`Micro::Case::Flow` - How to compose use cases?](#microcaseflow---how-to-compose-use-cases)
36
37
  - [Is it possible to compose a use case flow with other ones?](#is-it-possible-to-compose-a-use-case-flow-with-other-ones)
37
38
  - [Is it possible a flow accumulates its input and merges each success result to use as the argument of the next use cases?](#is-it-possible-a-flow-accumulates-its-input-and-merges-each-success-result-to-use-as-the-argument-of-the-next-use-cases)
@@ -44,7 +45,7 @@ The main project goals are:
44
45
  - [`Micro::Case::Result#on_exception`](#microcaseresulton_exception)
45
46
  - [`u-case/with_activemodel_validation` - How to validate use case attributes?](#u-casewith_activemodel_validation---how-to-validate-use-case-attributes)
46
47
  - [If I enabled the auto validation, is it possible to disable it only in specific use case classes?](#if-i-enabled-the-auto-validation-is-it-possible-to-disable-it-only-in-specific-use-case-classes)
47
- - [Kind::Validator](#kindvalidator)
48
+ - [`Kind::Validator`](#kindvalidator)
48
49
  - [Benchmarks](#benchmarks)
49
50
  - [`Micro::Case`](#microcase)
50
51
  - [Best overall](#best-overall)
@@ -431,6 +432,9 @@ result.value * 4 == accum # true
431
432
 
432
433
  #### How to use the `Micro::Case::Result#then` method?
433
434
 
435
+ This method allows you to create dynamic flows, so, with it,
436
+ you can add new use cases or flows to continue the result transformation. e.g:
437
+
434
438
  ```ruby
435
439
  class ForbidNegativeNumber < Micro::Case
436
440
  attribute :number
@@ -455,7 +459,6 @@ result1 =
455
459
  .call(number: -1)
456
460
  .then(Add3)
457
461
 
458
- result1.type # :error
459
462
  result1.value # {'number' => -1}
460
463
  result1.failure? # true
461
464
 
@@ -466,11 +469,26 @@ result2 =
466
469
  .call(number: 1)
467
470
  .then(Add3)
468
471
 
469
- result2.type # :ok
470
472
  result2.value # {'number' => 4}
471
473
  result2.success? # true
472
474
  ```
473
475
 
476
+ > **Note:** this method changes the [`Micro::Case::Result#transitions`](#how-to-understand-what-is-happening-during-a-flow-execution).
477
+
478
+ [⬆️ Back to Top](#table-of-contents-)
479
+
480
+ ##### How to make attributes data injection using this feature?
481
+
482
+ Pass a Hash as the second argument of the `Micro::Case::Result#then` method.
483
+
484
+ ```ruby
485
+ Todo::FindAllForUser
486
+ .call(user: current_user, params: params)
487
+ .then(Paginate)
488
+ .then(Serialize::PaginatedRelationAsJson, serializer: Todo::Serializer)
489
+ .on_success { |result| render_json(200, data: result[:todos]) }
490
+ ```
491
+
474
492
  [⬆️ Back to Top](#table-of-contents-)
475
493
 
476
494
  ### `Micro::Case::Flow` - How to compose use cases?
@@ -972,7 +990,7 @@ In functional programming errors/exceptions are handled as regular data, the ide
972
990
 
973
991
  To address this the `Micro::Case::Result` has a special hook `#on_exception` to helping you to handle the control flow in the case of exceptions.
974
992
 
975
- > **Note** this feature will work better if you use it with a `Micro::Case::Safe` use case/flow.
993
+ > **Note**: this feature will work better if you use it with a `Micro::Case::Safe` use case/flow.
976
994
 
977
995
  How does it work?
978
996
 
@@ -1092,7 +1110,7 @@ Multiply.call(a: 2, b: 'a')
1092
1110
 
1093
1111
  [⬆️ Back to Top](#table-of-contents-)
1094
1112
 
1095
- #### Kind::Validator
1113
+ #### `Kind::Validator`
1096
1114
 
1097
1115
  The [kind gem](https://github.com/serradura/kind) has a module to enable the validation of data type through [`ActiveModel validations`](https://guides.rubyonrails.org/active_model_basics.html#validations). So, when you require the `'u-case/with_activemodel_validation'`, this module will require the [`Kind::Validator`](https://github.com/serradura/kind#kindvalidator-activemodelvalidations).
1098
1116
 
@@ -44,11 +44,10 @@ module Micro
44
44
  end
45
45
 
46
46
  def self.__call_and_set_transition__(result, arg)
47
- if arg.respond_to?(:keys)
48
- result.__set_transitions_accessible_attributes__(arg.keys)
49
- end
47
+ input =
48
+ arg.is_a?(Hash) ? result.__set_transitions_accessible_attributes__(arg) : arg
50
49
 
51
- __new__(result, arg).call
50
+ __new__(result, input).call
52
51
  end
53
52
 
54
53
  FLOW_STEP = 'Flow_Step'.freeze
@@ -93,7 +93,7 @@ module Micro
93
93
  value = result.value
94
94
  input = value.is_a?(Hash) ? memo.tap { |data| data.merge!(value) } : value
95
95
 
96
- result.__set_transitions_accessible_attributes__(memo.keys)
96
+ result.__set_transitions_accessible_attributes__(memo)
97
97
 
98
98
  next_use_case_result(use_case, result, input)
99
99
  end
@@ -28,8 +28,8 @@ module Micro
28
28
  attr_reader :value, :type
29
29
 
30
30
  def initialize
31
- @__transitions__ = {}
32
- @__transitions_accessible_attributes__ = Set.new
31
+ @__transitions__ = []
32
+ @__transitions_accessible_attributes__ = {}
33
33
  end
34
34
 
35
35
  def __set__(is_success, value, type, use_case)
@@ -83,7 +83,7 @@ module Micro
83
83
  self
84
84
  end
85
85
 
86
- def then(arg = nil, &block)
86
+ def then(arg = nil, attributes = nil, &block)
87
87
  can_yield_self = respond_to?(:yield_self)
88
88
 
89
89
  if block
@@ -98,30 +98,33 @@ module Micro
98
98
 
99
99
  return self if failure?
100
100
 
101
- arg.__call_and_set_transition__(self, self.value)
101
+ input = attributes.is_a?(Hash) ? self.value.merge(attributes) : self.value
102
+
103
+ arg.__call_and_set_transition__(self, input)
102
104
  end
103
105
  end
104
106
 
105
107
  def transitions
106
- return [] if @__transitions__.empty?
107
-
108
- @__transitions__.map { |_use_case, transition| transition }
108
+ @__transitions__.clone
109
109
  end
110
110
 
111
- def __set_transitions_accessible_attributes__(attribute_names)
112
- return if @@transition_tracking_disabled
111
+ def __set_transitions_accessible_attributes__(attributes_data)
112
+ return attributes_data if @@transition_tracking_disabled
113
113
 
114
- __set_transitions_accessible_attributes__!(
115
- attribute_names.map!(&:to_sym)
116
- )
114
+ __set_transitions_accessible_attributes__!(attributes_data)
117
115
  end
118
116
 
119
117
  private
120
118
 
121
- def __set_transitions_accessible_attributes__!(attribute_names)
122
- @__transitions_accessible_attributes__.merge(
123
- attribute_names
124
- )
119
+ def __set_transitions_accessible_attributes__!(attributes_data)
120
+ attributes = Utils.symbolize_hash_keys(attributes_data)
121
+
122
+ __update_transitions_accessible_attributes__(attributes)
123
+ end
124
+
125
+ def __update_transitions_accessible_attributes__(attributes)
126
+ @__transitions_accessible_attributes__.merge!(attributes)
127
+ @__transitions_accessible_attributes__
125
128
  end
126
129
 
127
130
  def success_type?(expected_type)
@@ -140,16 +143,15 @@ module Micro
140
143
  use_case_class = @use_case.class
141
144
  use_case_attributes = Utils.symbolize_hash_keys(@use_case.attributes)
142
145
 
143
- __set_transitions_accessible_attributes__!(use_case_attributes.keys)
146
+ __update_transitions_accessible_attributes__(use_case_attributes)
144
147
 
145
148
  result = @success ? :success : :failure
146
- transition = {
149
+
150
+ @__transitions__ << {
147
151
  use_case: { class: use_case_class, attributes: use_case_attributes },
148
152
  result => { type: @type, value: @value },
149
- accessible_attributes: @__transitions_accessible_attributes__.to_a
153
+ accessible_attributes: @__transitions_accessible_attributes__.keys
150
154
  }
151
-
152
- @__transitions__[use_case_class] = transition
153
155
  end
154
156
  end
155
157
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  class Case
5
- VERSION = '2.5.0'.freeze
5
+ VERSION = '2.6.0'.freeze
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: u-case
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-26 00:00:00.000000000 Z
11
+ date: 2020-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kind