Class: MuchResult
- Inherits:
-
Object
show all
- Defined in:
- lib/much-result.rb,
lib/much-result/version.rb,
lib/much-result/aggregate.rb,
lib/much-result/transaction.rb
Defined Under Namespace
Classes: Aggregate, Transaction
Constant Summary
collapse
- SUCCESS =
"success"
- FAILURE =
"failure"
- Error =
Class.new(StandardError)
- Rollback =
Class.new(RuntimeError)
- VERSION =
"0.1.3"
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#all_failure_results ⇒ Object
-
#all_results ⇒ Object
-
#all_success_results ⇒ Object
-
#attribute_names ⇒ Object
-
#attributes ⇒ Object
-
#capture(backtrace: caller, **kargs) ⇒ Object
-
#capture!(backtrace: caller, **kargs) ⇒ Object
-
#capture_all(backtrace: caller, **kargs) ⇒ Object
-
#capture_all!(backtrace: caller, **kargs) ⇒ Object
-
#capture_exception ⇒ Object
Prefer any ‘#exception` set on the data.
-
#capture_for(value, backtrace: caller, **kargs) ⇒ Object
-
#capture_for!(value, backtrace: caller, **kargs) ⇒ Object
-
#capture_for_all(values, backtrace: caller, **kargs) ⇒ Object
-
#capture_for_all!(values, backtrace: caller, **kargs) ⇒ Object
-
#failure? ⇒ Boolean
-
#failure_sub_results ⇒ Object
-
#get_for_all_failure_results(attribute_name) ⇒ Object
-
#get_for_all_results(attribute_name) ⇒ Object
-
#get_for_all_success_results(attribute_name) ⇒ Object
-
#get_for_failure_sub_results(attribute_name) ⇒ Object
-
#get_for_sub_results(attribute_name) ⇒ Object
-
#get_for_success_sub_results(attribute_name) ⇒ Object
-
#initialize(result_value, description: nil, backtrace: caller, **kargs) ⇒ MuchResult
constructor
A new instance of MuchResult.
-
#inspect ⇒ Object
rubocop:enable Lint/UnusedMethodArgument.
-
#set(**kargs) ⇒ Object
-
#success? ⇒ Boolean
-
#success_sub_results ⇒ Object
-
#to_much_result(backtrace: caller, **kargs) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
Constructor Details
#initialize(result_value, description: nil, backtrace: caller, **kargs) ⇒ MuchResult
Returns a new instance of MuchResult.
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/much-result.rb', line 66
def initialize(result_value, description: nil, backtrace: caller, **kargs)
@result_value = result_value
@description = description
@backtrace = backtrace
set(**kargs)
@sub_results = []
reset_sub_results_cache
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
236
237
238
|
# File 'lib/much-result.rb', line 236
def method_missing(method, *args, &block)
@data.public_send(method, *args, &block)
end
|
Class Attribute Details
.default_transaction_receiver ⇒ Object
Returns the value of attribute default_transaction_receiver.
57
58
59
|
# File 'lib/much-result.rb', line 57
def default_transaction_receiver
@default_transaction_receiver
end
|
Instance Attribute Details
#backtrace ⇒ Object
Returns the value of attribute backtrace.
64
65
66
|
# File 'lib/much-result.rb', line 64
def backtrace
@backtrace
end
|
#description ⇒ Object
Returns the value of attribute description.
64
65
66
|
# File 'lib/much-result.rb', line 64
def description
@description
end
|
#sub_results ⇒ Object
Returns the value of attribute sub_results.
64
65
66
|
# File 'lib/much-result.rb', line 64
def sub_results
@sub_results
end
|
Class Method Details
.failure(backtrace: caller, **kargs) ⇒ Object
18
19
20
|
# File 'lib/much-result.rb', line 18
def self.failure(backtrace: caller, **kargs)
new(MuchResult::FAILURE, **kargs, backtrace: backtrace)
end
|
.for(value, backtrace: caller, **kargs) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/much-result.rb', line 22
def self.for(value, backtrace: caller, **kargs)
if value.respond_to?(:to_much_result)
return value.to_much_result(**kargs, backtrace: backtrace)
end
new(
!!value ? MuchResult::SUCCESS : MuchResult::FAILURE,
**kargs,
backtrace: backtrace,
)
end
|
.success(backtrace: caller, **kargs) ⇒ Object
14
15
16
|
# File 'lib/much-result.rb', line 14
def self.success(backtrace: caller, **kargs)
new(MuchResult::SUCCESS, **kargs, backtrace: backtrace)
end
|
.tap(backtrace: caller, **kargs) ⇒ Object
34
35
36
37
38
|
# File 'lib/much-result.rb', line 34
def self.tap(backtrace: caller, **kargs)
success(backtrace: backtrace, **kargs).tap do |result|
yield result if block_given?
end
end
|
.transaction(receiver = nil, backtrace: caller, **kargs, &block) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/much-result.rb', line 40
def self.transaction(receiver = nil, backtrace: caller, **kargs, &block)
if (transaction_receiver = receiver || default_transaction_receiver).nil?
raise(
ArgumentError,
"no receiver given and no default_transaction_receiver configured.",
)
end
MuchResult::Transaction.call(
transaction_receiver,
backtrace: backtrace,
**kargs,
&block
)
end
|
Instance Method Details
#all_failure_results ⇒ Object
173
174
175
176
177
|
# File 'lib/much-result.rb', line 173
def all_failure_results
@all_failure_results ||=
[*(self if failure?)] +
@sub_results.flat_map(&:all_failure_results)
end
|
#all_results ⇒ Object
161
162
163
164
165
|
# File 'lib/much-result.rb', line 161
def all_results
@all_results ||=
[self] +
@sub_results.flat_map(&:all_results)
end
|
#all_success_results ⇒ Object
167
168
169
170
171
|
# File 'lib/much-result.rb', line 167
def all_success_results
@all_success_results ||=
[*(self if success?)] +
@sub_results.flat_map(&:all_success_results)
end
|
#attribute_names ⇒ Object
86
87
88
|
# File 'lib/much-result.rb', line 86
def attribute_names
attributes.keys
end
|
#attributes ⇒ Object
82
83
84
|
# File 'lib/much-result.rb', line 82
def attributes
@data.to_h.reject{ |key, _| key.to_s.start_with?("much_result_") }
end
|
#capture(backtrace: caller, **kargs) ⇒ Object
131
132
133
|
# File 'lib/much-result.rb', line 131
def capture(backtrace: caller, **kargs)
capture_for((yield if block_given?), **kargs, backtrace: backtrace)
end
|
#capture!(backtrace: caller, **kargs) ⇒ Object
135
136
137
|
# File 'lib/much-result.rb', line 135
def capture!(backtrace: caller, **kargs)
capture_for!((yield if block_given?), **kargs, backtrace: backtrace)
end
|
#capture_all(backtrace: caller, **kargs) ⇒ Object
139
140
141
|
# File 'lib/much-result.rb', line 139
def capture_all(backtrace: caller, **kargs)
capture_for_all((yield if block_given?), **kargs, backtrace: backtrace)
end
|
#capture_all!(backtrace: caller, **kargs) ⇒ Object
143
144
145
|
# File 'lib/much-result.rb', line 143
def capture_all!(backtrace: caller, **kargs)
capture_for_all!((yield if block_given?), **kargs, backtrace: backtrace)
end
|
#capture_exception ⇒ Object
Prefer any ‘#exception` set on the data. Fallback to building an exception from the description/backtrace of the result.
149
150
151
|
# File 'lib/much-result.rb', line 149
def capture_exception
@data.exception || build_default_capture_exception
end
|
#capture_for(value, backtrace: caller, **kargs) ⇒ Object
106
107
108
109
110
111
|
# File 'lib/much-result.rb', line 106
def capture_for(value, backtrace: caller, **kargs)
self.class.for(value, backtrace: backtrace, **kargs).tap do |result|
@sub_results.push(result)
reset_sub_results_cache
end
end
|
#capture_for!(value, backtrace: caller, **kargs) ⇒ Object
113
114
115
116
117
|
# File 'lib/much-result.rb', line 113
def capture_for!(value, backtrace: caller, **kargs)
capture_for(value, **kargs, backtrace: backtrace).tap do |result|
raise(result.capture_exception) if result.failure?
end
end
|
#capture_for_all(values, backtrace: caller, **kargs) ⇒ Object
119
120
121
|
# File 'lib/much-result.rb', line 119
def capture_for_all(values, backtrace: caller, **kargs)
[*values].map{ |value| capture_for(value, **kargs, backtrace: backtrace) }
end
|
#capture_for_all!(values, backtrace: caller, **kargs) ⇒ Object
123
124
125
126
127
128
129
|
# File 'lib/much-result.rb', line 123
def capture_for_all!(values, backtrace: caller, **kargs)
capture_for_all(values, **kargs, backtrace: backtrace).tap do |results|
if (first_failure_result = results.detect(&:failure?))
raise(first_failure_result.capture_exception)
end
end
end
|
#failure? ⇒ Boolean
102
103
104
|
# File 'lib/much-result.rb', line 102
def failure?
!success?
end
|
#failure_sub_results ⇒ Object
157
158
159
|
# File 'lib/much-result.rb', line 157
def failure_sub_results
@failure_sub_results ||= @sub_results.select(&:failure?)
end
|
#get_for_all_failure_results(attribute_name) ⇒ Object
199
200
201
|
# File 'lib/much-result.rb', line 199
def get_for_all_failure_results(attribute_name)
MuchResult::Aggregate.call(all_failure_results.map(&attribute_name.to_sym))
end
|
#get_for_all_results(attribute_name) ⇒ Object
191
192
193
|
# File 'lib/much-result.rb', line 191
def get_for_all_results(attribute_name)
MuchResult::Aggregate.call(all_results.map(&attribute_name.to_sym))
end
|
#get_for_all_success_results(attribute_name) ⇒ Object
195
196
197
|
# File 'lib/much-result.rb', line 195
def get_for_all_success_results(attribute_name)
MuchResult::Aggregate.call(all_success_results.map(&attribute_name.to_sym))
end
|
#get_for_failure_sub_results(attribute_name) ⇒ Object
187
188
189
|
# File 'lib/much-result.rb', line 187
def get_for_failure_sub_results(attribute_name)
MuchResult::Aggregate.call(failure_sub_results.map(&attribute_name.to_sym))
end
|
#get_for_sub_results(attribute_name) ⇒ Object
179
180
181
|
# File 'lib/much-result.rb', line 179
def get_for_sub_results(attribute_name)
MuchResult::Aggregate.call(sub_results.map(&attribute_name.to_sym))
end
|
#get_for_success_sub_results(attribute_name) ⇒ Object
183
184
185
|
# File 'lib/much-result.rb', line 183
def get_for_success_sub_results(attribute_name)
MuchResult::Aggregate.call(success_sub_results.map(&attribute_name.to_sym))
end
|
#inspect ⇒ Object
rubocop:enable Lint/UnusedMethodArgument
209
210
211
212
213
214
215
|
# File 'lib/much-result.rb', line 209
def inspect
"#<#{self.class}:#{"0x0%x" % (object_id << 1)} "\
"#{success? ? "SUCCESS" : "FAILURE"} "\
"#{"@description=#{@description.inspect} " if @description}"\
"@sub_results=#{@sub_results.inspect} "\
"attribute_names: #{attribute_names}>"
end
|
#set(**kargs) ⇒ Object
77
78
79
80
|
# File 'lib/much-result.rb', line 77
def set(**kargs)
@data = ::OpenStruct.new((@data || {}).to_h.merge(**kargs))
self
end
|
#success? ⇒ Boolean
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/much-result.rb', line 90
def success?
if @success_predicate.nil?
@success_predicate =
@sub_results
.reduce(@result_value == MuchResult::SUCCESS) do |acc, result|
acc && result.success?
end
end
@success_predicate
end
|
#success_sub_results ⇒ Object
153
154
155
|
# File 'lib/much-result.rb', line 153
def success_sub_results
@success_sub_results ||= @sub_results.select(&:success?)
end
|
#to_much_result(backtrace: caller, **kargs) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
204
205
206
|
# File 'lib/much-result.rb', line 204
def to_much_result(backtrace: caller, **kargs)
set(**kargs)
end
|