Class: Micro::Case::Result
- Inherits:
-
Object
- Object
- Micro::Case::Result
show all
- Defined in:
- lib/micro/case/result.rb,
lib/micro/case/result/wrapper.rb,
lib/micro/case/result/contract.rb,
lib/micro/case/result/transitions.rb
Defined Under Namespace
Classes: Contract, Transitions, Wrapper
Constant Summary
collapse
- @@transitions_enabled =
true
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#[](key) ⇒ Object
-
#__set__(is_success, data, type, use_case) ⇒ Object
-
#__set_accessible_attributes__(arg) ⇒ Object
-
#accessible_attributes ⇒ Object
-
#deconstruct ⇒ Object
-
#deconstruct_keys(keys) ⇒ Object
-
#failure? ⇒ Boolean
-
#fetch(*args, &block) ⇒ Object
-
#fetch_values(*keys, &block) ⇒ Object
-
#initialize(transitions_mapper = Transitions::MapEverything) ⇒ Result
constructor
A new instance of Result.
-
#inspect ⇒ Object
-
#key?(key) ⇒ Boolean
-
#keys ⇒ Object
-
#on_exception(expected_exception = nil) ⇒ Object
-
#on_failure(expected_type = nil) {|hook_data, @use_case| ... } ⇒ Object
-
#on_success(expected_type = nil) {|hook_data, @use_case| ... } ⇒ Object
-
#on_unknown {|_self, @use_case| ... } ⇒ Object
-
#slice(*keys) ⇒ Object
-
#success? ⇒ Boolean
-
#then(use_case = nil, attributes = nil, &block) ⇒ Object
-
#to_ary ⇒ Object
-
#to_sym ⇒ Object
-
#transitions ⇒ Object
-
#unknown? ⇒ Boolean
-
#value?(value) ⇒ Boolean
-
#values_at(*keys) ⇒ Object
-
#|(arg) ⇒ Object
Constructor Details
#initialize(transitions_mapper = Transitions::MapEverything) ⇒ Result
Returns a new instance of Result.
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/micro/case/result.rb', line 24
def initialize(transitions_mapper = Transitions::MapEverything)
enable_transitions = @@transitions_enabled
@__is_unknown = true
@__accumulated_data = {}
@__tracked_use_cases = Set.new
@__accessible_attributes = {}
@__transitions = enable_transitions ? [] : Kind::Empty::ARRAY
@__transitions_mapper = transitions_mapper if enable_transitions
end
|
Instance Attribute Details
#data ⇒ Object
Also known as:
value
Returns the value of attribute data.
20
21
22
|
# File 'lib/micro/case/result.rb', line 20
def data
@data
end
|
#type ⇒ Object
Returns the value of attribute type.
20
21
22
|
# File 'lib/micro/case/result.rb', line 20
def type
@type
end
|
#use_case ⇒ Object
Returns the value of attribute use_case.
20
21
22
|
# File 'lib/micro/case/result.rb', line 20
def use_case
@use_case
end
|
Class Method Details
.transitions_enabled? ⇒ Boolean
16
17
18
|
# File 'lib/micro/case/result.rb', line 16
def self.transitions_enabled?
@@transitions_enabled
end
|
Instance Method Details
#[](key) ⇒ Object
79
80
81
|
# File 'lib/micro/case/result.rb', line 79
def [](key)
data[key]
end
|
#__set__(is_success, data, type, use_case) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/micro/case/result.rb', line 202
def __set__(is_success, data, type, use_case)
::Micro::Case.check.result_type!(type)
::Micro::Case.check.micro_case_instance!(use_case)
@__success, @type, @use_case = is_success, type, use_case
@data = FetchData.call(data).freeze
::Micro::Case.check.result_data!(@data, is_success, type, use_case)
@__accumulated_data.merge!(@data)
use_case_attributes = Utils::Hashes.symbolize_keys(@use_case.attributes)
unless @__tracked_use_cases.member?(use_case_class = @use_case.class)
@__tracked_use_cases.add(use_case_class)
__update_accessible_attributes(use_case_attributes)
end
__set_transition(use_case_attributes) unless @__transitions.frozen?
self
end
|
#__set_accessible_attributes__(arg) ⇒ Object
227
228
229
230
231
232
233
234
|
# File 'lib/micro/case/result.rb', line 227
def __set_accessible_attributes__(arg)
return arg unless arg.is_a?(Hash)
attributes = Utils::Hashes.symbolize_keys(arg)
__update_accessible_attributes(attributes)
__fetch_accessible_attributes
end
|
#accessible_attributes ⇒ Object
123
124
125
|
# File 'lib/micro/case/result.rb', line 123
def accessible_attributes
@__accessible_attributes.keys
end
|
#deconstruct ⇒ Object
53
54
55
|
# File 'lib/micro/case/result.rb', line 53
def deconstruct
[@__success ? :success : :failure, type, data]
end
|
#deconstruct_keys(keys) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/micro/case/result.rb', line 57
def deconstruct_keys(keys)
if keys.nil?
hash = { type: type, data: data, result: data, use_case: use_case, transitions: transitions }
hash[@__success ? :success : :failure] = type
return hash
end
hash = {}
keys.each do |key|
case key
when :type then hash[:type] = type
when :data then hash[:data] = data
when :result then hash[:result] = data
when :use_case then hash[:use_case] = use_case
when :transitions then hash[:transitions] = transitions
when :success then hash[:success] = type if @__success
when :failure then hash[:failure] = type if !@__success
end
end
hash
end
|
#failure? ⇒ Boolean
115
116
117
|
# File 'lib/micro/case/result.rb', line 115
def failure?
!success?
end
|
#fetch(*args, &block) ⇒ Object
95
96
97
|
# File 'lib/micro/case/result.rb', line 95
def fetch(*args, &block)
data.fetch(*args, &block)
end
|
#fetch_values(*keys, &block) ⇒ Object
99
100
101
|
# File 'lib/micro/case/result.rb', line 99
def fetch_values(*keys, &block)
Utils::Hashes.fetch_values(data, keys, &block)
end
|
#inspect ⇒ Object
36
37
38
39
40
41
42
43
|
# File 'lib/micro/case/result.rb', line 36
def inspect
pretty_type = @__success ? 'Success' : 'Failure'
instance_info = '%s (%s) type=:%s data=%s' % [pretty_type, self.class, @type, data]
transitions_info = ' transitions=%d' % [@__transitions.size] if Micro::Case::Result.transitions_enabled?
"#<#{instance_info}#{transitions_info}>"
end
|
#key?(key) ⇒ Boolean
87
88
89
|
# File 'lib/micro/case/result.rb', line 87
def key?(key)
data.key?(key)
end
|
#keys ⇒ Object
91
92
93
|
# File 'lib/micro/case/result.rb', line 91
def keys
data.keys
end
|
#on_exception(expected_exception = nil) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/micro/case/result.rb', line 149
def on_exception(expected_exception = nil)
if Config.instance.disable_safe_features
raise Error::SafeFeaturesDisabled.new('Micro::Case::Result#on_exception')
end
return self unless __failure_type?(:exception)
if !expected_exception || (Kind.is?(Exception, expected_exception) && data.fetch(:exception).is_a?(expected_exception))
@__is_unknown = false
yield(data, @use_case)
end
self
end
|
#on_failure(expected_type = nil) {|hook_data, @use_case| ... } ⇒ Object
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/micro/case/result.rb', line 138
def on_failure(expected_type = nil)
return self unless __failure_type?(expected_type)
@__is_unknown = false
hook_data = expected_type.nil? ? self : data
yield(hook_data, @use_case)
self
end
|
#on_success(expected_type = nil) {|hook_data, @use_case| ... } ⇒ Object
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/micro/case/result.rb', line 127
def on_success(expected_type = nil)
return self unless __success_type?(expected_type)
@__is_unknown = false
hook_data = expected_type.nil? ? self : data
yield(hook_data, @use_case)
self
end
|
#on_unknown {|_self, @use_case| ... } ⇒ Object
165
166
167
168
169
170
171
|
# File 'lib/micro/case/result.rb', line 165
def on_unknown
return self unless unknown?
yield(self, @use_case)
self
end
|
#slice(*keys) ⇒ Object
107
108
109
|
# File 'lib/micro/case/result.rb', line 107
def slice(*keys)
Utils::Hashes.slice(data, keys)
end
|
#success? ⇒ Boolean
111
112
113
|
# File 'lib/micro/case/result.rb', line 111
def success?
@__success
end
|
#then(use_case = nil, attributes = nil, &block) ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/micro/case/result.rb', line 173
def then(use_case = nil, attributes = nil, &block)
can_yield_self = respond_to?(:yield_self)
if block
raise INVALID_INVOCATION_OF_THE_THEN_METHOD if use_case
raise NotImplementedError if !can_yield_self
yield_self(&block)
else
return yield_self if !use_case && can_yield_self
return failure? ? self : __call_use_case(use_case, attributes)
end
end
|
#to_ary ⇒ Object
45
46
47
|
# File 'lib/micro/case/result.rb', line 45
def to_ary
[data, type]
end
|
#to_sym ⇒ Object
49
50
51
|
# File 'lib/micro/case/result.rb', line 49
def to_sym
@__success ? :success : :failure
end
|
#transitions ⇒ Object
191
192
193
|
# File 'lib/micro/case/result.rb', line 191
def transitions
@__transitions.dup
end
|
#unknown? ⇒ Boolean
119
120
121
|
# File 'lib/micro/case/result.rb', line 119
def unknown?
@__is_unknown
end
|
#value?(value) ⇒ Boolean
103
104
105
|
# File 'lib/micro/case/result.rb', line 103
def value?(value)
data.value?(value)
end
|
#values_at(*keys) ⇒ Object
83
84
85
|
# File 'lib/micro/case/result.rb', line 83
def values_at(*keys)
data.values_at(*keys)
end
|
#|(arg) ⇒ Object
187
188
189
|
# File 'lib/micro/case/result.rb', line 187
def |(arg)
failure? ? self : __call_use_case(arg)
end
|