Class: FastJsonapi::MultiToJson::Result
- Inherits:
-
Object
- Object
- FastJsonapi::MultiToJson::Result
- Defined in:
- lib/fast_jsonapi/multi_to_json.rb
Overview
Result object pattern is from johnnunemaker.com/resilience-in-ruby/ e.g. github.com/github/github-ds/blob/fbda5389711edfb4c10b6c6bad19311dfcb1bac1/lib/github/result.rb
Instance Method Summary collapse
-
#initialize(*rescued_exceptions) ⇒ Result
constructor
A new instance of Result.
- #ok? ⇒ Boolean
- #rescue ⇒ Object
- #value! ⇒ Object
Constructor Details
#initialize(*rescued_exceptions) ⇒ Result
Returns a new instance of Result.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/fast_jsonapi/multi_to_json.rb', line 16 def initialize(*rescued_exceptions) @rescued_exceptions = if rescued_exceptions.empty? [StandardError] else rescued_exceptions end @value = yield @error = nil rescue *rescued_exceptions => e @error = e end |
Instance Method Details
#ok? ⇒ Boolean
29 30 31 |
# File 'lib/fast_jsonapi/multi_to_json.rb', line 29 def ok? @error.nil? end |
#rescue ⇒ Object
41 42 43 44 45 |
# File 'lib/fast_jsonapi/multi_to_json.rb', line 41 def rescue return self if ok? Result.new(*@rescued_exceptions) { yield(@error) } end |
#value! ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/fast_jsonapi/multi_to_json.rb', line 33 def value! if ok? @value else raise @error end end |