Class: RustyKey::RescuedProc

Inherits:
Object
  • Object
show all
Defined in:
lib/rusty_key/exception.rb

Instance Method Summary collapse

Constructor Details

#initialize(wrapped, *types, &handler) ⇒ RescuedProc

Returns a new instance of RescuedProc.



42
43
44
45
46
# File 'lib/rusty_key/exception.rb', line 42

def initialize(wrapped, *types, &handler)
  self.handlers = merge_handlers(handler, *types, initial: {})
  self.prc = wrapped
  self.result = nil
end

Instance Method Details

#callObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rusty_key/exception.rb', line 58

def call
  begin
    self.result = prc.call
  rescue Exception => e
    if match = handlers.find { |type, handler| e.is_a? type }&.last
      match&.call(e)
    else
      raise e
    end
  else
    otherwise&.call
  ensure
    finally&.call
  end
  result
end

#else(&handler) ⇒ Object



53
54
55
56
# File 'lib/rusty_key/exception.rb', line 53

def else(&handler)
  self.otherwise = handler
  self
end

#ensure(&handler) ⇒ Object



79
80
81
82
# File 'lib/rusty_key/exception.rb', line 79

def ensure(&handler)
  self.finally = handler
  self
end

#ensure!(&handler) ⇒ Object



84
85
86
87
# File 'lib/rusty_key/exception.rb', line 84

def ensure!(&handler)
  self.ensure(&handler)
  self.call
end

#rescue(*types, &handler) ⇒ Object



48
49
50
51
# File 'lib/rusty_key/exception.rb', line 48

def rescue(*types, &handler)
  self.handlers = merge_handlers(handler, *types)
  self
end

#to_procObject



75
76
77
# File 'lib/rusty_key/exception.rb', line 75

def to_proc
  -> { self.call }
end