Class: Philiprehberger::Try::Failure
- Inherits:
-
Object
- Object
- Philiprehberger::Try::Failure
- Defined in:
- lib/philiprehberger/try.rb
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
Instance Method Summary collapse
- #deconstruct_keys(_keys) ⇒ Object
- #failure? ⇒ Boolean
- #filter ⇒ Object
- #flat_map ⇒ Object
-
#initialize(error) ⇒ Failure
constructor
A new instance of Failure.
- #map ⇒ Object
- #map_error ⇒ Object
- #on(exception_class, &block) ⇒ Object
- #on_error {|@error| ... } ⇒ Object
- #or_else(default) ⇒ Object
- #or_try ⇒ Object
- #recover ⇒ Object
- #success? ⇒ Boolean
- #tap {|_self| ... } ⇒ Object
- #transform(on_success: nil, on_failure: nil) ⇒ Object
- #value ⇒ Object
- #value! ⇒ Object
Constructor Details
#initialize(error) ⇒ Failure
Returns a new instance of Failure.
120 121 122 123 |
# File 'lib/philiprehberger/try.rb', line 120 def initialize(error) @error = error freeze end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
118 119 120 |
# File 'lib/philiprehberger/try.rb', line 118 def error @error end |
Instance Method Details
#deconstruct_keys(_keys) ⇒ Object
191 192 193 |
# File 'lib/philiprehberger/try.rb', line 191 def deconstruct_keys(_keys) { success: false, error: @error } end |
#failure? ⇒ Boolean
137 138 139 |
# File 'lib/philiprehberger/try.rb', line 137 def failure? true end |
#filter ⇒ Object
178 179 180 |
# File 'lib/philiprehberger/try.rb', line 178 def filter self end |
#flat_map ⇒ Object
164 165 166 |
# File 'lib/philiprehberger/try.rb', line 164 def flat_map self end |
#map ⇒ Object
160 161 162 |
# File 'lib/philiprehberger/try.rb', line 160 def map self end |
#map_error ⇒ Object
172 173 174 175 176 |
# File 'lib/philiprehberger/try.rb', line 172 def map_error new_error = yield @error new_error = RuntimeError.new(new_error.to_s) unless new_error.is_a?(Exception) Failure.new(new_error) end |
#on(exception_class, &block) ⇒ Object
149 150 151 152 153 |
# File 'lib/philiprehberger/try.rb', line 149 def on(exception_class, &block) return Try.call { block.call(@error) } if @error.is_a?(exception_class) self end |
#on_error {|@error| ... } ⇒ Object
155 156 157 158 |
# File 'lib/philiprehberger/try.rb', line 155 def on_error yield @error self end |
#or_else(default) ⇒ Object
141 142 143 |
# File 'lib/philiprehberger/try.rb', line 141 def or_else(default) Success.new(default) end |
#or_try ⇒ Object
145 146 147 |
# File 'lib/philiprehberger/try.rb', line 145 def or_try(&) Try.call(&) end |
#recover ⇒ Object
168 169 170 |
# File 'lib/philiprehberger/try.rb', line 168 def recover Try.call { yield @error } end |
#success? ⇒ Boolean
133 134 135 |
# File 'lib/philiprehberger/try.rb', line 133 def success? false end |
#tap {|_self| ... } ⇒ Object
182 183 184 185 |
# File 'lib/philiprehberger/try.rb', line 182 def tap yield self self end |
#transform(on_success: nil, on_failure: nil) ⇒ Object
187 188 189 |
# File 'lib/philiprehberger/try.rb', line 187 def transform(on_success: nil, on_failure: nil) on_failure ? Try.call { on_failure.call(@error) } : self end |
#value ⇒ Object
125 126 127 |
# File 'lib/philiprehberger/try.rb', line 125 def value nil end |
#value! ⇒ Object
129 130 131 |
# File 'lib/philiprehberger/try.rb', line 129 def value! raise @error end |