Class: Ytry::Failure

Inherits:
Object
  • Object
show all
Includes:
Try
Defined in:
lib/ytry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Try

ary_to_type, #each, #flat_map, #flatten, #get_or_else, #grep, #inspect, #map, #or_else, #reject, #select, zip, #zip, #|

Constructor Details

#initialize(value) ⇒ Failure

Returns a new instance of Failure.



126
127
128
# File 'lib/ytry.rb', line 126

def initialize value
  @error = value.freeze
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



125
126
127
# File 'lib/ytry.rb', line 125

def error
  @error
end

Instance Method Details

#==(other) ⇒ Object



133
134
135
# File 'lib/ytry.rb', line 133

def == other
  other.is_a?(Failure) && self.error == other.error
end

#===(other) ⇒ Object



136
137
138
# File 'lib/ytry.rb', line 136

def === other
  other.is_a?(Failure) && self.error === other.error
end

#empty?Boolean

Returns:

  • (Boolean)


130
# File 'lib/ytry.rb', line 130

def empty?() true end

#getObject

Raises:



129
# File 'lib/ytry.rb', line 129

def get() raise @error end

#on_failureObject



139
140
141
142
143
# File 'lib/ytry.rb', line 139

def on_failure
  return enum_for(__method__) unless block_given?
  Try { yield @error }
  return self
end

#recover(&block) ⇒ Object

Raises:

  • (ArgumentError)


144
145
146
147
148
# File 'lib/ytry.rb', line 144

def recover &block
  raise ArgumentError, 'missing block' unless block_given?
  candidate = Success.new(@error).map &block
  (!candidate.empty? && candidate.get.nil?) ? self : candidate
end

#recover_with(&block) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/ytry.rb', line 149

def recover_with &block
  candidate = self.recover(&block)
  if (!candidate.empty? && !candidate.get.is_a?(Try))
    raise(Try.invalid_argument('Block should evaluate to an instance of Try', candidate.get))
  else
    candidate.flatten
  end
end

#to_aryObject



132
# File 'lib/ytry.rb', line 132

def to_ary() [] end

#to_sObject



131
# File 'lib/ytry.rb', line 131

def to_s() "Failure(#{@error.inspect})" end