Module: RescueFrom::Rescuable
- Defined in:
- lib/rescue_from/rescuable.rb
Class Method Summary collapse
Instance Method Summary collapse
- #inherited(subclass) ⇒ Object
- #method_added(name) ⇒ Object
- #relabel(*patterns, to:, &message_generator) ⇒ Object
- #rescue_from(*patterns, &handler) ⇒ Object
- #should_rescue_in?(_method_name) ⇒ Boolean
Class Method Details
.extended(base) ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/rescue_from/rescuable.rb', line 5 def self.extended base overrider = Module.new do extend Overrider end base.const_set :RescueFrom_Overrider, overrider base.prepend overrider end |
Instance Method Details
#inherited(subclass) ⇒ Object
14 15 16 17 18 |
# File 'lib/rescue_from/rescuable.rb', line 14 def inherited subclass super subclass.extend Rescuable end |
#method_added(name) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rescue_from/rescuable.rb', line 40 def method_added name super return unless should_rescue_in? name overrider = const_get :RescueFrom_Overrider return if overrider.method_defined? name method_without_rescue = instance_method name overrider.define_method :"#{name}_without_rescue" do |*args, **kwargs, &block| method_without_rescue.bind_call(self, *args, **kwargs, &block) end # rubocop:disable Lint/RescueException overrider.define_method name do |*args, **kwargs, &block| super(*args, **kwargs, &block) rescue Exception => e self.class .ancestors .lazy .filter_map { |mod| mod.const_get :RescueFrom_Overrider if mod.const_defined? :RescueFrom_Overrider } .filter_map { |ancestor_overrider| ancestor_overrider.handler_for e } .first .otherwise { raise e } .therefore { |handler| instance_exec(e, &handler) } end # rubocop:enable Lint/RescueException end |
#relabel(*patterns, to:, &message_generator) ⇒ Object
30 31 32 33 34 |
# File 'lib/rescue_from/rescuable.rb', line 30 def relabel *patterns, to:, & rescue_from(*patterns) do |e| raise to, instance_exec(e, &) end end |
#rescue_from(*patterns, &handler) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/rescue_from/rescuable.rb', line 20 def rescue_from *patterns, &handler raise ArgumentError, 'always provide a handler when calling rescue_from' unless block_given? overrider = const_get :RescueFrom_Overrider patterns.each do |pattern| overrider.handlers[pattern] = handler end end |
#should_rescue_in?(_method_name) ⇒ Boolean
36 37 38 |
# File 'lib/rescue_from/rescuable.rb', line 36 def should_rescue_in? _method_name true end |