Class: Philiprehberger::Try::Success

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Success

Returns a new instance of Success.



41
42
43
44
# File 'lib/philiprehberger/try.rb', line 41

def initialize(value)
  @value = value
  freeze
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



39
40
41
# File 'lib/philiprehberger/try.rb', line 39

def value
  @value
end

Instance Method Details

#deconstruct_keys(_keys) ⇒ Object



112
113
114
# File 'lib/philiprehberger/try.rb', line 112

def deconstruct_keys(_keys)
  { success: true, value: @value }
end

#failure?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/philiprehberger/try.rb', line 54

def failure?
  false
end

#filterObject



97
98
99
100
101
# File 'lib/philiprehberger/try.rb', line 97

def filter
  return Failure.new(ArgumentError.new('filter condition not met')) unless yield @value

  self
end

#flat_mapObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/philiprehberger/try.rb', line 78

def flat_map
  result = yield @value
  raise TypeError, 'flat_map block must return Success or Failure' unless result.is_a?(Success) || result.is_a?(Failure)

  result
rescue TypeError
  raise
rescue StandardError => e
  Failure.new(e)
end

#mapObject



74
75
76
# File 'lib/philiprehberger/try.rb', line 74

def map
  Try.call { yield @value }
end

#map_errorObject



93
94
95
# File 'lib/philiprehberger/try.rb', line 93

def map_error
  self
end

#on(_exception_class) ⇒ Object



66
67
68
# File 'lib/philiprehberger/try.rb', line 66

def on(_exception_class)
  self
end

#on_errorObject



70
71
72
# File 'lib/philiprehberger/try.rb', line 70

def on_error
  self
end

#or_else(_default) ⇒ Object



58
59
60
# File 'lib/philiprehberger/try.rb', line 58

def or_else(_default)
  self
end

#or_tryObject



62
63
64
# File 'lib/philiprehberger/try.rb', line 62

def or_try
  self
end

#recoverObject



89
90
91
# File 'lib/philiprehberger/try.rb', line 89

def recover
  self
end

#success?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/philiprehberger/try.rb', line 50

def success?
  true
end

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

Yields:

  • (_self)

Yield Parameters:



103
104
105
106
# File 'lib/philiprehberger/try.rb', line 103

def tap
  yield self
  self
end

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



108
109
110
# File 'lib/philiprehberger/try.rb', line 108

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

#value!Object



46
47
48
# File 'lib/philiprehberger/try.rb', line 46

def value!
  @value
end