Module: Either

Included in:
Failure, Success
Defined in:
lib/monadic/either.rb

Overview

Chain various method calls

Defined Under Namespace

Classes: Chain

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.chain(initial = nil, &block) ⇒ Object



3
4
5
# File 'lib/monadic/either.rb', line 3

def self.chain(initial=nil, &block)
  Either::Chain.new(&block).call(initial)
end

Instance Method Details

#==(other) ⇒ Object



43
44
45
46
# File 'lib/monadic/either.rb', line 43

def ==(other)
  return false unless self.class === other
  return other.fetch == @value
end

#bind(proc = nil, &block) ⇒ Object Also known as: >=, +



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/monadic/either.rb', line 21

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

Returns:

  • (Boolean)


11
12
13
# File 'lib/monadic/either.rb', line 11

def failure?
  is_a? Failure
end

#fetch(default = @value) ⇒ Object Also known as: _



15
16
17
18
# File 'lib/monadic/either.rb', line 15

def fetch(default=@value)
  return default if failure?
  return @value
end

#success?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/monadic/either.rb', line 7

def success?
  is_a? Success
end

#to_sObject



39
40
41
# File 'lib/monadic/either.rb', line 39

def to_s
  "#{self.class.name}(#{@value.nil? ? 'nil' : @value.to_s})"
end