Class: Resonad::Success

Inherits:
Resonad show all
Defined in:
lib/resonad.rb

Constant Summary

Constants inherited from Resonad

Mixin, NIL_FAILURE, NIL_SUCCESS, VERSION

Constants included from PublicMixin

PublicMixin::Failure, PublicMixin::Success

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resonad

#and_then, #bad?, #failed?, #failure?, #if_bad, #if_failed, #if_failure, #if_ok, #if_success, #if_successful, #map_value, #ok?, #on_bad, #on_failed, #on_ok, #on_successful, #or_else, #otherwise, rescuing_from, #successful?, #when_bad, #when_failed, #when_failure, #when_ok, #when_success, #when_successful

Methods included from PublicMixin

#Failure, #Success, #failure, #success

Constructor Details

#initialize(value) ⇒ Success

Returns a new instance of Success.



16
17
18
19
# File 'lib/resonad.rb', line 16

def initialize(value)
  @value = value
  freeze
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/resonad.rb', line 6

def value
  @value
end

Class Method Details

.[](value = nil) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/resonad.rb', line 8

def self.[](value = nil)
  if nil == value
    NIL_SUCCESS
  else
    new(value)
  end
end

Instance Method Details

#deconstructObject



59
60
61
# File 'lib/resonad.rb', line 59

def deconstruct
  [:success, value]
end

#deconstruct_keys(_) ⇒ Object



63
64
65
# File 'lib/resonad.rb', line 63

def deconstruct_keys(_)
  { value: value }
end

#errorObject

Raises:



34
35
36
# File 'lib/resonad.rb', line 34

def error
  raise NonExistentError, "Success resonads do not have errors"
end

#flat_map {|value| ... } ⇒ Object

Yields:



51
52
53
# File 'lib/resonad.rb', line 51

def flat_map
  yield value
end

#flat_map_errorObject



55
56
57
# File 'lib/resonad.rb', line 55

def flat_map_error
  self
end

#mapObject



38
39
40
41
42
43
44
45
# File 'lib/resonad.rb', line 38

def map
  new_value = yield(value)
  if new_value.__id__ == value.__id__
    self
  else
    self.class.new(new_value)
  end
end

#map_errorObject



47
48
49
# File 'lib/resonad.rb', line 47

def map_error
  self
end

#on_failureObject



30
31
32
# File 'lib/resonad.rb', line 30

def on_failure
  self
end

#on_success {|value| ... } ⇒ Object

Yields:



25
26
27
28
# File 'lib/resonad.rb', line 25

def on_success
  yield value
  self
end

#success?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/resonad.rb', line 21

def success?
  true
end