u-case 3.0.0.rc4 → 3.0.0.rc5

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.
@@ -18,10 +18,25 @@ module Micro
18
18
 
19
19
  include Micro::Attributes.without(:strict_initialize)
20
20
 
21
- def self.call(options = {})
21
+ def self.call(options = Kind::Empty::HASH)
22
22
  new(options).__call__
23
23
  end
24
24
 
25
+ def self.then(use_case = nil, &block)
26
+ can_yield_self = respond_to?(:yield_self)
27
+
28
+ if block
29
+ raise Error::InvalidInvocationOfTheThenMethod if use_case
30
+ raise NotImplementedError if !can_yield_self
31
+
32
+ yield_self(&block)
33
+ else
34
+ return yield_self if !use_case && can_yield_self
35
+
36
+ self.call.then(use_case)
37
+ end
38
+ end
39
+
25
40
  def self.to_proc
26
41
  Proc.new { |arg| call(arg) }
27
42
  end
@@ -85,7 +100,7 @@ module Micro
85
100
  @__flow = __flow_builder__.build(args)
86
101
  end
87
102
 
88
- FLOW_STEP = 'Flow_Step'.freeze
103
+ FLOW_STEP = 'Self'.freeze
89
104
 
90
105
  private_constant :FLOW_STEP
91
106
 
@@ -196,11 +211,7 @@ module Micro
196
211
  private_constant :MapFailureType
197
212
  end
198
213
 
199
- def self.case?(arg)
200
- (arg.is_a?(Class) && arg < Case) || arg.is_a?(Case)
201
- end
202
-
203
214
  def self.case_or_flow?(arg)
204
- case?(arg) || arg.is_a?(Cases::Flow)
215
+ (arg.is_a?(Class) && arg < Case) || arg.is_a?(Cases::Flow)
205
216
  end
206
217
  end
@@ -30,6 +30,18 @@ module Micro
30
30
  data.values_at(*keys)
31
31
  end
32
32
 
33
+ def key?(key)
34
+ data.key?(key)
35
+ end
36
+
37
+ def value?(value)
38
+ data.value?(value)
39
+ end
40
+
41
+ def slice(*keys)
42
+ Utils.slice_hash(data, keys)
43
+ end
44
+
33
45
  def success?
34
46
  @success
35
47
  end
@@ -83,14 +95,17 @@ module Micro
83
95
  return failure? ? self : __call_proc(use_case, expected: 'then(-> {})'.freeze)
84
96
  end
85
97
 
86
- # TODO: Test the then method with a Micro::Cases.{flow,safe_flow}() instance.
87
98
  raise Error::InvalidInvocationOfTheThenMethod unless ::Micro.case_or_flow?(use_case)
88
99
 
89
100
  return self if failure?
90
101
 
91
102
  input = attributes.is_a?(Hash) ? self.data.merge(attributes) : self.data
92
103
 
93
- use_case.__call_and_set_transition__(self, input)
104
+ if use_case.is_a?(::Micro::Cases::Flow)
105
+ use_case.call!(input: input, result: self)
106
+ else
107
+ use_case.__call_and_set_transition__(self, input)
108
+ end
94
109
  end
95
110
  end
96
111
 
@@ -117,7 +132,7 @@ module Micro
117
132
 
118
133
  def __set__(is_success, data, type, use_case)
119
134
  raise Error::InvalidResultType unless type.is_a?(Symbol)
120
- raise Error::InvalidUseCase unless ::Micro.case?(use_case)
135
+ raise Error::InvalidUseCase unless use_case.is_a?(::Micro::Case)
121
136
 
122
137
  @success, @type, @use_case = is_success, type, use_case
123
138
 
@@ -14,6 +14,13 @@ module Micro
14
14
  end
15
15
  end
16
16
  end
17
+ def self.slice_hash(hash, keys)
18
+ if Kind::Of::Hash(hash).respond_to?(:slice)
19
+ hash.slice(*keys)
20
+ else
21
+ hash.select { |key, _value| keys.include?(key) }
22
+ end
23
+ end
17
24
  end
18
25
  end
19
26
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  class Case
5
- VERSION = '3.0.0.rc4'.freeze
5
+ VERSION = '3.0.0.rc5'.freeze
6
6
  end
7
7
  end
@@ -29,48 +29,42 @@ module Micro
29
29
  @next_use_cases = use_cases[1..-1]
30
30
  end
31
31
 
32
- def call(arg = {})
33
- memo = arg.is_a?(Hash) ? arg.dup : {}
32
+ def call!(input:, result:)
33
+ memo = input.is_a?(Hash) ? input.dup : Kind::Empty::HASH
34
34
 
35
- first_result = __first_use_case_result(arg)
35
+ first_result = @first_use_case.__call_and_set_transition__(result, input)
36
36
 
37
37
  return first_result if @next_use_cases.empty?
38
38
 
39
39
  __next_use_cases_result(first_result, memo)
40
40
  end
41
41
 
42
+ def call(input = Kind::Empty::HASH)
43
+ call!(input: input, result: Case::Result.new)
44
+ end
45
+
42
46
  alias __call__ call
43
47
 
44
48
  def to_proc
45
49
  Proc.new { |arg| call(arg) }
46
50
  end
47
51
 
48
- private
49
-
50
- def __is_a_result?(arg)
51
- arg.is_a?(Case::Result)
52
- end
53
-
54
- def __call_arg(arg)
55
- output = arg.__call__
52
+ def then(use_case = nil, &block)
53
+ can_yield_self = respond_to?(:yield_self)
56
54
 
57
- __is_a_result?(output) ? output.value : output
58
- end
55
+ if block
56
+ raise Error::InvalidInvocationOfTheThenMethod if use_case
57
+ raise NotImplementedError if !can_yield_self
59
58
 
60
- def __first_use_case_input(arg)
61
- return __call_arg(arg) if ::Micro.case_or_flow?(arg)
62
- return arg.value if __is_a_result?(arg)
59
+ yield_self(&block)
60
+ else
61
+ return yield_self if !use_case && can_yield_self
63
62
 
64
- arg
63
+ self.call.then(use_case)
65
64
  end
65
+ end
66
66
 
67
- def __first_use_case_result(arg)
68
- input = __first_use_case_input(arg)
69
-
70
- result = Case::Result.new
71
-
72
- @first_use_case.__call_and_set_transition__(result, input)
73
- end
67
+ private
74
68
 
75
69
  def __next_use_case_result(use_case, result, input)
76
70
  use_case.__new__(result, input).__call__
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ['Rodrigo Serradura']
10
10
  spec.email = ['rodrigo.serradura@gmail.com']
11
11
 
12
- spec.summary = %q{Create simple and powerful use cases as objects.}
13
- spec.description = %q{Create simple and powerful use cases as objects.}
12
+ spec.summary = %q{Create simple and powerful use cases as Ruby objects.}
13
+ spec.description = %q{Create simple and powerful use cases as Ruby objects.}
14
14
  spec.homepage = 'https://github.com/serradura/u-case'
15
15
  spec.license = 'MIT'
16
16
 
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.rc4
4
+ version: 3.0.0.rc5
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-08-02 00:00:00.000000000 Z
11
+ date: 2020-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kind
@@ -72,7 +72,7 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '13.0'
75
- description: Create simple and powerful use cases as objects.
75
+ description: Create simple and powerful use cases as Ruby objects.
76
76
  email:
77
77
  - rodrigo.serradura@gmail.com
78
78
  executables: []
@@ -87,6 +87,7 @@ files:
87
87
  - Gemfile
88
88
  - LICENSE.txt
89
89
  - README.md
90
+ - README.pt-BR.md
90
91
  - Rakefile
91
92
  - bin/console
92
93
  - bin/setup
@@ -128,5 +129,5 @@ requirements: []
128
129
  rubygems_version: 3.0.6
129
130
  signing_key:
130
131
  specification_version: 4
131
- summary: Create simple and powerful use cases as objects.
132
+ summary: Create simple and powerful use cases as Ruby objects.
132
133
  test_files: []