Class: Deterministic::Either::AttemptAll
- Inherits:
-
Object
- Object
- Deterministic::Either::AttemptAll
show all
- Defined in:
- lib/deterministic/either/attempt_all.rb
Defined Under Namespace
Classes: EitherExpectedError
Instance Method Summary
collapse
Constructor Details
#initialize(context, &block) ⇒ AttemptAll
Returns a new instance of AttemptAll.
10
11
12
13
14
|
# File 'lib/deterministic/either/attempt_all.rb', line 10
def initialize(context, &block)
@context = context || self
@tries = []
instance_eval(&block)
end
|
Instance Method Details
#call(initial = nil) ⇒ Object
16
17
18
19
20
|
# File 'lib/deterministic/either/attempt_all.rb', line 16
def call(initial=nil)
result = @tries.inject(Success(initial)) do |acc, try|
acc.success? ? acc << try.call(acc) : acc
end
end
|
#let(sym = nil, &block) ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/deterministic/either/attempt_all.rb', line 35
def let(sym=nil, &block)
@tries << ->(acc) {
@context.instance_exec(acc, &block).tap do |value|
raise EitherExpectedError unless value.is_a? Either
end
}
end
|
#try(&block) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/deterministic/either/attempt_all.rb', line 22
def try(&block)
try_p = ->(acc) {
begin
value = @context.instance_exec(acc.value, &block)
Success(value)
rescue => ex
Failure(ex)
end
}
@tries << try_p
end
|