Module: DebugLogging::InstanceNotifierModulizer

Defined in:
lib/debug_logging/instance_notifier_modulizer.rb

Class Method Summary collapse

Class Method Details

.to_mod(methods_to_notify: nil, payload: nil, config: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/debug_logging/instance_notifier_modulizer.rb', line 5

def self.to_mod(methods_to_notify: nil, payload: nil, config: nil)
  Module.new do
    methods_to_notify, payload, config_opts = DebugLogging::Util.extract_payload_and_config(
      method_names: Array(methods_to_notify),
      payload: payload,
      config: config,
    )
    Array(methods_to_notify).each do |method_to_notify|
      method_to_notify, method_payload, method_config_opts = DebugLogging::Util.extract_payload_and_config(
        method_names: method_to_notify,
        payload: payload,
        config: config_opts,
      )
      define_method(method_to_notify) do |*args, &block|
        config_proxy = DebugLogging::Util.config_proxy_finder(
          scope: self.class,
          config_opts: method_config_opts,
          method_name: method_to_notify,
          proxy_ref: "inm",
        ) do |config_proxy|
          ActiveSupport::Notifications.subscribe(
            DebugLogging::ArgumentPrinter.debug_event_name_to_s(method_to_notify: method_to_notify),
          ) do |*args|
            config_proxy&.log do
              DebugLogging::LogSubscriber.log_event(ActiveSupport::Notifications::Event.new(*args))
            end
          end
        end
        paydirt = DebugLogging::Util.payload_instance_variable_hydration(scope: self, payload: method_payload)
        ActiveSupport::Notifications.instrument(
          DebugLogging::ArgumentPrinter.debug_event_name_to_s(method_to_notify: method_to_notify),
          debug_args: args,
          config_proxy: config_proxy,
          **paydirt,
        ) do
          begin
            # TODO: I need to split logic based on Ruby version.
            # TODO: Use VersionGem's minimum Ruby version check
            # TODO: Switch between positional, and kwargs argument handling
            # See: https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
            super(*args, &block)
          rescue StandardError => e
            if config_proxy.error_handler_proc
              config_proxy.error_handler_proc.call(config_proxy, e, self, method_to_notify, args)
            else
              raise e
            end
          end
        end
      end
    end
  end
end