u-case 3.0.0.rc2 → 3.0.0.rc3

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: dfc46204c461937d19681ae26dca7846a8d36720b735d759023b4c1369ef2a9a
4
- data.tar.gz: 21bfd7bc9d2c2a13c901672eac77f15da4b7975aeeea925b3f94ff7e36dd1161
3
+ metadata.gz: de27799d2fbf95d3399af2caa3053038fd9817c6f800747213eb8ee3722604b5
4
+ data.tar.gz: dc2f1b40964d646978d28ffb44c049a58ddca614440e296ff5fa7f35e347def1
5
5
  SHA512:
6
- metadata.gz: 1e6992400490ef920d9ad855ebf698174ee2fcf9ab555db880ab2dea065360b921af7e38e4be5eb74e28c484862a25ca476beba9e2daebbc668bae07ef1b1853
7
- data.tar.gz: d081ddf7bf5b3209f31c385060fd8dbbc9f72a8b1a05e553b1666df2f2de1550859017e9aa85aa329118bdef965f390a1637348c2a7549139e462e3cb091c250
6
+ metadata.gz: 90af10df71576124850933260f703e7a2f9c6a67df5e5add06d7d657de34a291879e279eec59795bfd548c0e0f898dc3795657de1de524e3aac16dddf25b24cf
7
+ data.tar.gz: e5b374929b87a7a5a0f604afa5742195910809b9198c11d01110bb8cf9f2540de73aa80fedb4432357ee86b96fe5e81b91a003fd410da989ac5b22b9ad40aa30
data/README.md CHANGED
@@ -22,7 +22,7 @@ The main project goals are:
22
22
 
23
23
  Version | Documentation
24
24
  --------- | -------------
25
- 3.0.0.rc2 | https://github.com/serradura/u-case/blob/master/README.md
25
+ 3.0.0.rc3 | https://github.com/serradura/u-case/blob/master/README.md
26
26
  2.6.0 | https://github.com/serradura/u-case/blob/v2.x/README.md
27
27
  1.1.0 | https://github.com/serradura/u-case/blob/v1.x/README.md
28
28
 
@@ -47,6 +47,7 @@ Version | Documentation
47
47
  - [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)
48
48
  - [How to understand what is happening during a flow execution?](#how-to-understand-what-is-happening-during-a-flow-execution)
49
49
  - [`Micro::Case::Result#transitions` schema](#microcaseresulttransitions-schema)
50
+ - [Is it possible disable the `Micro::Case::Result#transitions`?](#is-it-possible-disable-the-microcaseresulttransitions)
50
51
  - [Is it possible to declare a flow which includes the use case itself?](#is-it-possible-to-declare-a-flow-which-includes-the-use-case-itself)
51
52
  - [`Micro::Case::Strict` - What is a strict use case?](#microcasestrict---what-is-a-strict-use-case)
52
53
  - [`Micro::Case::Safe` - Is there some feature to auto handle exceptions inside of a use case or flow?](#microcasesafe---is-there-some-feature-to-auto-handle-exceptions-inside-of-a-use-case-or-flow)
@@ -55,6 +56,7 @@ Version | Documentation
55
56
  - [`u-case/with_activemodel_validation` - How to validate use case attributes?](#u-casewith_activemodel_validation---how-to-validate-use-case-attributes)
56
57
  - [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)
57
58
  - [`Kind::Validator`](#kindvalidator)
59
+ - [`Micro::Case.config`](#microcaseconfig)
58
60
  - [Benchmarks](#benchmarks)
59
61
  - [`Micro::Case` (v2.6.0)](#microcase-v260)
60
62
  - [Best overall](#best-overall)
@@ -164,12 +166,13 @@ result.value # { number: 6 }
164
166
  A `Micro::Case::Result` stores the use cases output data. These are their main methods:
165
167
  - `#success?` returns true if is a successful result.
166
168
  - `#failure?` returns true if is an unsuccessful result.
167
- - `#data` the result data itself.
169
+ - `#use_case` returns the use case responsible for it. This feature is handy to handle a flow failure (this topic will be covered ahead).
168
170
  - `#type` a Symbol which gives meaning for the result, this is useful to declare different types of failures or success.
171
+ - `#data` the result data itself.
172
+ - `#[]` and `#values_at` are shortcuts to access the `#data` values.
169
173
  - `#on_success` or `#on_failure` are hook methods that help you to define the application flow.
170
- - `#use_case` if is a failure result, the use case responsible for it will be accessible through this method. This feature is handy to handle a flow failure (this topic will be covered ahead).
171
174
  - `#then` this method will allow applying a new use case if the current result was a success. The idea of this feature is to allow the creation of dynamic flows.
172
- - `#[]` and `#values_at` are shortcuts to access the `#data` values.
175
+ - `#transitions` returns an array with all of transformations wich a result [has during a flow](#how-to-understand-what-is-happening-during-a-flow-execution).
173
176
 
174
177
  > **Note:** for backward compatibility, you could use the `#value` method as an alias of `#data` method.
175
178
 
@@ -207,7 +210,7 @@ result = Divide.call(a: 2, b: 2)
207
210
  result.type # :ok
208
211
  result.data # { number: 1 }
209
212
  result.success? # true
210
- result.use_case # raises `Micro::Case::Error::InvalidAccessToTheUseCaseObject: only a failure result can access its own use case`
213
+ result.use_case # #<Divide:0x0000 @__attributes={"a"=>2, "b"=>2}, @a=2, @b=2, @__result=...>
211
214
 
212
215
  # Failure result (type == :error)
213
216
 
@@ -216,7 +219,7 @@ bad_result = Divide.call(a: 2, b: '2')
216
219
  bad_result.type # :error
217
220
  bad_result.data # { invalid_attributes: { "b"=>"2" } }
218
221
  bad_result.failure? # true
219
- bad_result.use_case # #<Divide:0x0000 @__attributes={"a"=>2, "b"=>"2"}, @a=2, @b="2", @__result=#<Micro::Case::Result:0x0000 @use_case=#<Divide:0x0000 ...>, @type=:error, @value={"b"=>"2"}, @success=false>
222
+ bad_result.use_case # #<Divide:0x0000 @__attributes={"a"=>2, "b"=>"2"}, @a=2, @b="2", @__result=...>
220
223
 
221
224
  # Failure result (type == :exception)
222
225
 
@@ -834,8 +837,6 @@ And look up the `accessible_attributes` property, it shows whats attributes are
834
837
 
835
838
  > **Note:** The [`Micro::Case::Result#then`](#how-to-use-the-microcaseresultthen-method) increments the `Micro::Case::Result#transitions`.
836
839
 
837
- PS: Use the `Micro::Case::Result.disable_transition_tracking` feature toggle to disable this feature (use once, because it is global) and increase the use cases' performance.
838
-
839
840
  ##### `Micro::Case::Result#transitions` schema
840
841
  ```ruby
841
842
  [
@@ -856,6 +857,10 @@ PS: Use the `Micro::Case::Result.disable_transition_tracking` feature toggle to
856
857
  ]
857
858
  ```
858
859
 
860
+ ##### Is it possible disable the `Micro::Case::Result#transitions`?
861
+
862
+ Answer: Yes, it is! You can use the `Micro::Case.config` to do this. [Link to](#microcaseconfig) this section.
863
+
859
864
  #### Is it possible to declare a flow which includes the use case itself?
860
865
 
861
866
  Answer: Yes, it is! You can use the `self.call!` macro. e.g:
@@ -1073,18 +1078,22 @@ class Multiply < Micro::Case
1073
1078
  Success result: { number: a * b }
1074
1079
  end
1075
1080
  end
1081
+ ```
1076
1082
 
1077
- #
1078
- # But if do you want an automatic way to fail
1079
- # your use cases on validation errors, you can use:
1083
+ But if do you want an automatic way to fail your use cases on validation errors, you can:
1080
1084
 
1081
- # In some file. e.g: A Rails initializer
1082
- require 'u-case/with_activemodel_validation' # or require 'micro/case/with_validation'
1085
+ 1. **require 'u-case/with_activemodel_validation'** mode
1083
1086
 
1084
- # In the Gemfile
1085
- gem 'u-case', require: 'u-case/with_activemodel_validation'
1087
+ ```ruby
1088
+ gem 'u-case', require: 'u-case/with_activemodel_validation'
1089
+ ```
1086
1090
 
1087
- # Using this approach, you can rewrite the previous example with less code. e.g:
1091
+ 2. Use the `Micro::Case.config` to enable it. [Link to](#microcaseconfig) this section.
1092
+
1093
+ Using this approach, you can rewrite the previous example with less code. e.g:
1094
+
1095
+ ```ruby
1096
+ require 'u-case/with_activemodel_validation'
1088
1097
 
1089
1098
  class Multiply < Micro::Case
1090
1099
  attributes :a, :b
@@ -1154,6 +1163,23 @@ class Todo::List::AddItem < Micro::Case
1154
1163
  end
1155
1164
  ```
1156
1165
 
1166
+ ## `Micro::Case.config`
1167
+
1168
+ The idea of this feature is to allow the configuration of some `u-case` features/modules.
1169
+ I recommend you use it only once in your codebase. e.g. In a Rails initializer.
1170
+
1171
+ You can see below, which are all of the available configurations with their default values:
1172
+
1173
+ ```ruby
1174
+ Micro::Case.config do |config|
1175
+ # Use ActiveModel to auto-validate your use cases' attributes.
1176
+ config.enable_activemodel_validations = false
1177
+
1178
+ # Use to enable/disable the `Micro::Case::Results#transitions` tracking.
1179
+ config.enable_transitions = true
1180
+ end
1181
+ ```
1182
+
1157
1183
  ## Benchmarks
1158
1184
 
1159
1185
  ### `Micro::Case` (v2.6.0)
@@ -12,11 +12,16 @@ module Micro
12
12
  require 'micro/case/error'
13
13
  require 'micro/case/safe'
14
14
  require 'micro/case/strict'
15
+ require 'micro/case/config'
15
16
 
16
17
  require 'micro/cases'
17
18
 
18
19
  include Micro::Attributes.without(:strict_initialize)
19
20
 
21
+ def self.config
22
+ yield(Config.instance)
23
+ end
24
+
20
25
  def self.call(options = {})
21
26
  new(options).call
22
27
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+
5
+ module Micro
6
+ class Case
7
+ class Config
8
+ include Singleton
9
+
10
+ def enable_activemodel_validation=(value)
11
+ return unless Kind::Of::Boolean(value)
12
+
13
+ require 'micro/case/with_activemodel_validation'
14
+ end
15
+
16
+ def enable_transitions=(value)
17
+ Micro::Case::Result.class_variable_set(
18
+ :@@transition_tracking_disabled, !Kind::Of::Boolean(value)
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
@@ -47,10 +47,6 @@ module Micro
47
47
  def initialize; super('Invalid invocation of the Micro::Case::Result#then method'); end
48
48
  end
49
49
 
50
- class InvalidAccessToTheUseCaseObject < StandardError
51
- def initialize; super('only a failure result can access its use case object'.freeze); end
52
- end
53
-
54
50
  def self.by_wrong_usage?(exception)
55
51
  exception.is_a?(InvalidResult) || exception.is_a?(UnexpectedResult) || exception.is_a?(ArgumentError)
56
52
  end
@@ -9,11 +9,7 @@ module Micro
9
9
 
10
10
  @@transition_tracking_disabled = false
11
11
 
12
- def self.disable_transition_tracking
13
- @@transition_tracking_disabled = true
14
- end
15
-
16
- attr_reader :type, :data
12
+ attr_reader :type, :data, :use_case
17
13
 
18
14
  alias_method :value, :data
19
15
 
@@ -42,14 +38,12 @@ module Micro
42
38
  !success?
43
39
  end
44
40
 
45
- def use_case
46
- return @use_case if failure?
41
+ def on_success(expected_type = nil)
42
+ return self unless success_type?(expected_type)
47
43
 
48
- raise Error::InvalidAccessToTheUseCaseObject
49
- end
44
+ hook_data = expected_type.nil? ? self : data
50
45
 
51
- def on_success(expected_type = nil)
52
- yield(data) if success_type?(expected_type)
46
+ yield(hook_data, @use_case)
53
47
 
54
48
  self
55
49
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  class Case
5
- VERSION = '3.0.0.rc2'.freeze
5
+ VERSION = '3.0.0.rc3'.freeze
6
6
  end
7
7
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'kind/active_model/validation'
4
+
3
5
  require 'micro/case'
4
6
 
5
7
  module Micro
@@ -1,5 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'kind/active_model/validation'
4
-
5
3
  require 'micro/case/with_activemodel_validation'
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: 3.0.0.rc2
4
+ version: 3.0.0.rc3
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-07-28 00:00:00.000000000 Z
11
+ date: 2020-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kind
@@ -91,6 +91,7 @@ files:
91
91
  - bin/console
92
92
  - bin/setup
93
93
  - lib/micro/case.rb
94
+ - lib/micro/case/config.rb
94
95
  - lib/micro/case/error.rb
95
96
  - lib/micro/case/result.rb
96
97
  - lib/micro/case/safe.rb