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.



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

Returns:

  • (Boolean)


29
30
31
# File 'lib/fast_jsonapi/multi_to_json.rb', line 29

def ok?
  @error.nil?
end

#rescueObject



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