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.3.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)


162
163
164
# File 'lib/resonad.rb', line 162

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



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/resonad.rb', line 150

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(&block) ⇒ Object



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

def and_then(&block); flat_map(&block); end

#bad?Boolean

Returns:

  • (Boolean)


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

def bad?; failure?; end

#failed?Boolean

Returns:

  • (Boolean)


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

def failed?; failure?; end

#failure?Boolean

Returns:

  • (Boolean)


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

def failure?
  not success?
end

#flat_mapObject

Raises:

  • (NotImplementedError)


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

def flat_map
  raise NotImplementedError, "should be implemented in subclass"
end

#flat_map_errorObject

Raises:

  • (NotImplementedError)


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

def flat_map_error
  raise NotImplementedError, "should be implemented in subclass"
end

#if_bad(&block) ⇒ Object



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

def if_bad(&block); on_failure(&block); end

#if_failed(&block) ⇒ Object



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

def if_failed(&block); on_failure(&block); end

#if_failure(&block) ⇒ Object



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

def if_failure(&block); on_failure(&block); end

#if_ok(&block) ⇒ Object



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

def if_ok(&block); on_success(&block); end

#if_success(&block) ⇒ Object



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

def if_success(&block); on_success(&block); end

#if_successful(&block) ⇒ Object



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

def if_successful(&block); on_success(&block); end

#map(&block) ⇒ Object

Raises:

  • (NotImplementedError)


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

def map(&block)
  raise NotImplementedError, "should be implemented in subclass"
end

#map_value(&block) ⇒ Object



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

def map_value(&block); map(&block); end

#ok?Boolean

Returns:

  • (Boolean)


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

def ok?; success?; end

#on_bad(&block) ⇒ Object



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

def on_bad(&block); on_failure(&block); end

#on_failed(&block) ⇒ Object



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

def on_failed(&block); on_failure(&block); end

#on_failure(&block) ⇒ Object

Raises:

  • (NotImplementedError)


206
207
208
# File 'lib/resonad.rb', line 206

def on_failure(&block)
  raise NotImplementedError, "should be implemented in subclass"
end

#on_ok(&block) ⇒ Object



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

def on_ok(&block); on_success(&block); end

#on_success(&block) ⇒ Object

Raises:

  • (NotImplementedError)


194
195
196
# File 'lib/resonad.rb', line 194

def on_success(&block)
  raise NotImplementedError, "should be implemented in subclass"
end

#on_successful(&block) ⇒ Object



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

def on_successful(&block); on_success(&block); end

#or_else(&block) ⇒ Object



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

def or_else(&block); flat_map_error(&block); end

#otherwise(&block) ⇒ Object



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

def otherwise(&block); flat_map_error(&block); end

#success?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

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

#successful?Boolean

Returns:

  • (Boolean)


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

def successful?; success?; end

#when_bad(&block) ⇒ Object



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

def when_bad(&block); on_failure(&block); end

#when_failed(&block) ⇒ Object



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

def when_failed(&block); on_failure(&block); end

#when_failure(&block) ⇒ Object



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

def when_failure(&block); on_failure(&block); end

#when_ok(&block) ⇒ Object



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

def when_ok(&block); on_success(&block); end

#when_success(&block) ⇒ Object



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

def when_success(&block); on_success(&block); end

#when_successful(&block) ⇒ Object



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

def when_successful(&block); on_success(&block); end