Class: Philiprehberger::Try::Failure

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/try.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error) ⇒ Failure

Returns a new instance of Failure.



120
121
122
123
# File 'lib/philiprehberger/try.rb', line 120

def initialize(error)
  @error = error
  freeze
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



118
119
120
# File 'lib/philiprehberger/try.rb', line 118

def error
  @error
end

Instance Method Details

#deconstruct_keys(_keys) ⇒ Object



191
192
193
# File 'lib/philiprehberger/try.rb', line 191

def deconstruct_keys(_keys)
  { success: false, error: @error }
end

#failure?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/philiprehberger/try.rb', line 137

def failure?
  true
end

#filterObject



178
179
180
# File 'lib/philiprehberger/try.rb', line 178

def filter
  self
end

#flat_mapObject



164
165
166
# File 'lib/philiprehberger/try.rb', line 164

def flat_map
  self
end

#mapObject



160
161
162
# File 'lib/philiprehberger/try.rb', line 160

def map
  self
end

#map_errorObject



172
173
174
175
176
# File 'lib/philiprehberger/try.rb', line 172

def map_error
  new_error = yield @error
  new_error = RuntimeError.new(new_error.to_s) unless new_error.is_a?(Exception)
  Failure.new(new_error)
end

#on(exception_class, &block) ⇒ Object



149
150
151
152
153
# File 'lib/philiprehberger/try.rb', line 149

def on(exception_class, &block)
  return Try.call { block.call(@error) } if @error.is_a?(exception_class)

  self
end

#on_error {|@error| ... } ⇒ Object

Yields:



155
156
157
158
# File 'lib/philiprehberger/try.rb', line 155

def on_error
  yield @error
  self
end

#or_else(default) ⇒ Object



141
142
143
# File 'lib/philiprehberger/try.rb', line 141

def or_else(default)
  Success.new(default)
end

#or_tryObject



145
146
147
# File 'lib/philiprehberger/try.rb', line 145

def or_try(&)
  Try.call(&)
end

#recoverObject



168
169
170
# File 'lib/philiprehberger/try.rb', line 168

def recover
  Try.call { yield @error }
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  false
end

#tap {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



182
183
184
185
# File 'lib/philiprehberger/try.rb', line 182

def tap
  yield self
  self
end

#transform(on_success: nil, on_failure: nil) ⇒ Object



187
188
189
# File 'lib/philiprehberger/try.rb', line 187

def transform(on_success: nil, on_failure: nil)
  on_failure ? Try.call { on_failure.call(@error) } : self
end

#valueObject



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

def value
  nil
end

#value!Object

Raises:



129
130
131
# File 'lib/philiprehberger/try.rb', line 129

def value!
  raise @error
end