Module: Either
Overview
Chain various method calls
Defined Under Namespace
Classes: Chain, NoEitherError
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.chain(initial = nil, &block) ⇒ Object
5
6
7
|
# File 'lib/monadic/either.rb', line 5
def self.chain(initial=nil, &block)
Either::Chain.new(&block).call(initial)
end
|
Instance Method Details
#==(other) ⇒ Object
46
47
48
49
|
# File 'lib/monadic/either.rb', line 46
def ==(other)
return false unless self.class === other
return other.fetch == @value
end
|
#bind(proc = nil, &block) ⇒ Object
Also known as:
>=, +, chain
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/monadic/either.rb', line 23
def bind(proc=nil, &block)
return self if failure?
begin
result = if proc && proc.arity == 0
then proc.call
else (proc || block).call(@value)
end
result ||= Failure(nil)
result = Either(result) unless result.is_a? Either
result
rescue Exception => ex
Failure(ex)
end
end
|
#failure? ⇒ Boolean
13
14
15
|
# File 'lib/monadic/either.rb', line 13
def failure?
is_a? Failure
end
|
#fetch(default = @value) ⇒ Object
Also known as:
_
17
18
19
20
|
# File 'lib/monadic/either.rb', line 17
def fetch(default=@value)
return default if failure?
return @value
end
|
#success? ⇒ Boolean
9
10
11
|
# File 'lib/monadic/either.rb', line 9
def success?
is_a? Success
end
|
#to_s ⇒ Object
42
43
44
|
# File 'lib/monadic/either.rb', line 42
def to_s
"#{self.class.name}(#{@value.nil? ? 'nil' : @value.to_s})"
end
|