Class: Resonad

Inherits:
Object
  • Object
show all
Extended by:
PublicMixin
Defined in:
lib/resonad.rb,
lib/resonad/version.rb

Direct Known Subclasses

Failure, Success

Defined Under Namespace

Modules: PublicMixin Classes: Failure, NonExistentError, NonExistentValue, Success

Constant Summary collapse

Mixin =
PublicMixin.dup.tap do |mixin|
  mixin.module_eval do
    private(*public_instance_methods)
    private_constant(*constants)
  end
end
NIL_SUCCESS =
Success.new(nil)
NIL_FAILURE =
Failure.new(nil)
VERSION =
'1.4.0'

Constants included from PublicMixin

PublicMixin::Failure, PublicMixin::Success

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PublicMixin

Failure, Success, failure, success

Constructor Details

#initialize(*args) ⇒ Resonad

Returns a new instance of Resonad.

Raises:

  • (NotImplementedError)


166
167
168
# File 'lib/resonad.rb', line 166

def initialize(*args)
  raise NotImplementedError, "This is an abstract class. Use Resonad::Success or Resonad::Failure instead."
end

Class Method Details

.rescuing_from(*exception_classes) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# 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

Instance Method Details

#and_then(callable = nil, &block) ⇒ Object



188
# File 'lib/resonad.rb', line 188

def and_then(callable=nil, &block); __flat_map(callable_from_args(callable, block)); end

#bad?Boolean

Returns:

  • (Boolean)


180
# File 'lib/resonad.rb', line 180

def bad?; failure?; end

#failed?Boolean

Returns:

  • (Boolean)


179
# File 'lib/resonad.rb', line 179

def failed?; failure?; end

#failure?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/resonad.rb', line 176

def failure?
  not success?
end

#flat_map(callable = nil, &block) ⇒ Object



187
# 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) ⇒ Object



190
# 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) ⇒ Object



208
# 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) ⇒ Object



211
# 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) ⇒ Object



205
# 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) ⇒ Object



198
# 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) ⇒ Object



195
# 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) ⇒ Object



201
# File 'lib/resonad.rb', line 201

def if_successful(callable=nil, &block);   __on_success(callable_from_args(callable, block)); end

#map(callable = nil, &block) ⇒ Object



182
# File 'lib/resonad.rb', line 182

def map(callable=nil, &block);       __map(callable_from_args(callable, block)); end

#map_error(callable = nil, &block) ⇒ Object



185
# 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) ⇒ Object



183
# File 'lib/resonad.rb', line 183

def map_value(callable=nil, &block); __map(callable_from_args(callable, block)); end

#ok?Boolean

Returns:

  • (Boolean)


174
# File 'lib/resonad.rb', line 174

def ok?; success?; end

#on_bad(callable = nil, &block) ⇒ Object



207
# 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) ⇒ Object



210
# 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) ⇒ Object



204
# 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) ⇒ Object



197
# 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) ⇒ Object



194
# 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) ⇒ Object



200
# 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) ⇒ Object



191
# 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) ⇒ Object



192
# File 'lib/resonad.rb', line 192

def otherwise(callable=nil, &block);      __flat_map_error(callable_from_args(callable, block)); end

#success?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


170
171
172
# File 'lib/resonad.rb', line 170

def success?
  raise NotImplementedError, "should be implemented in subclass"
end

#successful?Boolean

Returns:

  • (Boolean)


173
# File 'lib/resonad.rb', line 173

def successful?; success?; end

#when_bad(callable = nil, &block) ⇒ Object



209
# 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) ⇒ Object



212
# 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) ⇒ Object



206
# 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) ⇒ Object



199
# 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) ⇒ Object



196
# 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) ⇒ Object



202
# File 'lib/resonad.rb', line 202

def when_successful(callable=nil, &block); __on_success(callable_from_args(callable, block)); end