class Resonad

Constants

Mixin
NIL_FAILURE
NIL_SUCCESS
VERSION

Public Class Methods

new(*args) click to toggle source
# File lib/resonad.rb, line 166
def initialize(*args)
  raise NotImplementedError, "This is an abstract class. Use Resonad::Success or Resonad::Failure instead."
end
rescuing_from(*exception_classes) { || ... } click to toggle source
# File lib/resonad.rb, line 154
def self.rescuing_from(*exception_classes)
  Success(yield)
rescue Exception => e
  if exception_classes.empty?
    Failure(e) # rescue from all exceptions
  elsif exception_classes.any? { |klass| e.is_a?(klass) }
    Failure(e) # rescue from specified exception type
  else
    raise # reraise unhandled exception
  end
end

Public Instance Methods

and_then(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 188
def and_then(callable=nil, &block); __flat_map(callable_from_args(callable, block)); end
bad?() click to toggle source
# File lib/resonad.rb, line 180
def bad?; failure?; end
failed?() click to toggle source
# File lib/resonad.rb, line 179
def failed?; failure?; end
failure?() click to toggle source
# File lib/resonad.rb, line 176
def failure?
  not success?
end
flat_map(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 187
def flat_map(callable=nil, &block); __flat_map(callable_from_args(callable, block)); end
flat_map_error(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 190
def flat_map_error(callable=nil, &block); __flat_map_error(callable_from_args(callable, block)); end
if_bad(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 208
def if_bad(callable=nil, &block);       __on_failure(callable_from_args(callable, block)); end
if_failed(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 211
def if_failed(callable=nil, &block);    __on_failure(callable_from_args(callable, block)); end
if_failure(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 205
def if_failure(callable=nil, &block);   __on_failure(callable_from_args(callable, block)); end
if_ok(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 198
def if_ok(callable=nil, &block);           __on_success(callable_from_args(callable, block)); end
if_success(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 195
def if_success(callable=nil, &block);      __on_success(callable_from_args(callable, block)); end
if_successful(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 201
def if_successful(callable=nil, &block);   __on_success(callable_from_args(callable, block)); end
map(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 182
def map(callable=nil, &block);       __map(callable_from_args(callable, block)); end
map_error(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 185
def map_error(callable=nil, &block); __map_error(callable_from_args(callable, block)); end
map_value(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 183
def map_value(callable=nil, &block); __map(callable_from_args(callable, block)); end
ok?() click to toggle source
# File lib/resonad.rb, line 174
def ok?; success?; end
on_bad(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 207
def on_bad(callable=nil, &block);       __on_failure(callable_from_args(callable, block)); end
on_failed(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 210
def on_failed(callable=nil, &block);    __on_failure(callable_from_args(callable, block)); end
on_failure(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 204
def on_failure(callable=nil, &block);   __on_failure(callable_from_args(callable, block)); end
on_ok(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 197
def on_ok(callable=nil, &block);           __on_success(callable_from_args(callable, block)); end
on_success(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 194
def on_success(callable=nil, &block);      __on_success(callable_from_args(callable, block)); end
on_successful(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 200
def on_successful(callable=nil, &block);   __on_success(callable_from_args(callable, block)); end
or_else(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 191
def or_else(callable=nil, &block);        __flat_map_error(callable_from_args(callable, block)); end
otherwise(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 192
def otherwise(callable=nil, &block);      __flat_map_error(callable_from_args(callable, block)); end
success?() click to toggle source
# File lib/resonad.rb, line 170
def success?
  raise NotImplementedError, "should be implemented in subclass"
end
successful?() click to toggle source
# File lib/resonad.rb, line 173
def successful?; success?; end
when_bad(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 209
def when_bad(callable=nil, &block);     __on_failure(callable_from_args(callable, block)); end
when_failed(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 212
def when_failed(callable=nil, &block);  __on_failure(callable_from_args(callable, block)); end
when_failure(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 206
def when_failure(callable=nil, &block); __on_failure(callable_from_args(callable, block)); end
when_ok(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 199
def when_ok(callable=nil, &block);         __on_success(callable_from_args(callable, block)); end
when_success(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 196
def when_success(callable=nil, &block);    __on_success(callable_from_args(callable, block)); end
when_successful(callable=nil, &block) click to toggle source
# File lib/resonad.rb, line 202
def when_successful(callable=nil, &block); __on_success(callable_from_args(callable, block)); end

Private Instance Methods

__flat_map(callable) click to toggle source
# File lib/resonad.rb, line 223
def __flat_map(callable)
  raise NotImplementedError, "should be implemented in subclass"
end
__flat_map_error(callable) click to toggle source
# File lib/resonad.rb, line 227
def __flat_map_error(callable)
  raise NotImplementedError, "should be implemented in subclass"
end
__map(callable) click to toggle source
# File lib/resonad.rb, line 219
def __map(callable)
  raise NotImplementedError, "should be implemented in subclass"
end
__on_failure(callable) click to toggle source
# File lib/resonad.rb, line 235
def __on_failure(callable)
  raise NotImplementedError, "should be implemented in subclass"
end
__on_success(callable) click to toggle source
# File lib/resonad.rb, line 231
def __on_success(callable)
  raise NotImplementedError, "should be implemented in subclass"
end
callable_from_args(positional, block) click to toggle source
# File lib/resonad.rb, line 239
def callable_from_args(positional, block)
  if block
    if positional
      raise ArgumentError, "expected _either_ a callable or a block argument, but _both_ were given"
    else
      block
    end
  else
    if positional
      positional
    else
      raise ArgumentError, "expected either a callable or a block argument, but neither were given"
    end
  end
end