Module: SolidUseCase::Either
- Defined in:
- lib/solid_use_case/either.rb,
lib/solid_use_case/either/error_struct.rb,
lib/solid_use_case/either/class_methods.rb
Defined Under Namespace
Modules: ClassMethods
Classes: ErrorStruct
Instance Method Summary
collapse
Instance Method Details
#attempt ⇒ Object
38
39
40
41
42
|
# File 'lib/solid_use_case/either.rb', line 38
def attempt
attempt_all do
try { yield }
end
end
|
#catch(required, *exceptions) ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/solid_use_case/either.rb', line 44
def catch(required, *exceptions)
exceptions << required
result = attempt_all do
try { yield }
end
if result.is_a?(Failure) && exceptions.any?
raise result.value unless exceptions.include?(result.value)
end
end
|
#check_exists(val, error = :not_found) ⇒ Object
Also known as:
maybe_continue
# # # # # Helpers # # # # # #
30
31
32
33
34
35
36
|
# File 'lib/solid_use_case/either.rb', line 30
def check_exists(val, error=:not_found)
if val.nil?
fail(error)
else
continue(val)
end
end
|
#fail(type, data = {}) ⇒ Object
54
55
56
57
|
# File 'lib/solid_use_case/either.rb', line 54
def fail(type, data={})
data[:type] = type
Failure(ErrorStruct.new(data))
end
|
#run(inputs) ⇒ Object
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/solid_use_case/either.rb', line 4
def run(inputs)
steps = self.class.instance_variable_get("@__steps").clone
current_result = Success(inputs)
return current_result unless steps
while steps.count > 0
next_step = steps.shift
current_result = current_result.and_then do
if next_step.is_a?(Class) && (next_step.respond_to? :can_run_either?) && next_step.can_run_either?
next_step.run(current_result.value)
elsif next_step.is_a?(Symbol)
self.send(next_step, current_result.value)
else
raise "Invalid step type: #{next_step.inspect}"
end
end
end
current_result
end
|