Module: SolidUseCase::Composable
- Defined in:
- lib/solid_use_case/composable.rb,
lib/solid_use_case/composable/error_struct.rb,
lib/solid_use_case/composable/class_methods.rb
Defined Under Namespace
Modules: ClassMethods
Classes: ErrorStruct
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(includer) ⇒ Object
4
5
6
7
|
# File 'lib/solid_use_case/composable.rb', line 4
def self.included(includer)
includer.send :include, Deterministic::CoreExt::Either
includer.extend ClassMethods
end
|
Instance Method Details
#attempt ⇒ Object
43
44
45
46
47
|
# File 'lib/solid_use_case/composable.rb', line 43
def attempt
attempt_all do
try { yield }
end
end
|
#check_exists(val, error = :not_found) ⇒ Object
Also known as:
maybe_continue
# # # # # Helpers # # # # # #
35
36
37
38
39
40
41
|
# File 'lib/solid_use_case/composable.rb', line 35
def check_exists(val, error=:not_found)
if val.nil?
fail(error)
else
continue(val)
end
end
|
#fail(type, data = {}) ⇒ Object
49
50
51
52
|
# File 'lib/solid_use_case/composable.rb', line 49
def fail(type, data={})
data[:type] = type
Failure(ErrorStruct.new(data))
end
|
#run(inputs) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/solid_use_case/composable.rb', line 9
def run(inputs)
steps = self.class.instance_variable_get("@__steps").clone
result = Success(inputs)
return result unless steps
while steps.count > 0
next_step = steps.shift
if next_step.is_a?(Class) && (next_step.respond_to? :composable?) && next_step.composable?
subresult = next_step.run(result.value)
elsif next_step.is_a?(Symbol)
subresult = self.send(next_step, result.value)
else
raise "Invalid step type: #{next_step.inspect}"
end
result = result.and(subresult)
end
result
end
|