Class: FastJsonapi::MultiToJson::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_jsonapi/multi_to_json.rb

Overview

Instance Method Summary collapse

Constructor Details

#initialize(*rescued_exceptions) ⇒ Result

Returns a new instance of Result.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fast_jsonapi/multi_to_json.rb', line 14

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

Returns:

  • (Boolean)


27
28
29
# File 'lib/fast_jsonapi/multi_to_json.rb', line 27

def ok?
  @error.nil?
end

#rescueObject



39
40
41
42
43
# File 'lib/fast_jsonapi/multi_to_json.rb', line 39

def rescue
  return self if ok?

  Result.new(*@rescued_exceptions) { yield(@error) }
end

#value!Object



31
32
33
34
35
36
37
# File 'lib/fast_jsonapi/multi_to_json.rb', line 31

def value!
  if ok?
    @value
  else
    raise @error
  end
end