Module: SweetNotifications::ControllerRuntime

Included in:
SweetNotifications
Defined in:
lib/sweet_notifications/controller_runtime.rb

Overview

Controller runtime tracking

Instance Method Summary collapse

Instance Method Details

#controller_runtime(name, log_subscriber) ⇒ Module

Define a controller runtime logger for a LogSusbcriber

Parameters:

  • name (String)

    title for logging

Returns:

  • (Module)

    controller runtime tracking mixin



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
# File 'lib/sweet_notifications/controller_runtime.rb', line 11

def controller_runtime(name, log_subscriber)
  runtime_attr = "#{name.to_s.underscore}_runtime".to_sym
  Module.new do
    extend ActiveSupport::Concern
    attr_internal runtime_attr

    protected

    define_method :process_action do |action, *args|
      log_subscriber.reset_runtime
      super(action, *args)
    end

    define_method :cleanup_view_runtime do |&block|
      runtime_before_render = log_subscriber.reset_runtime
      send("#{runtime_attr}=",
           (send(runtime_attr) || 0) + runtime_before_render)
      runtime = super(&block)
      runtime_after_render = log_subscriber.reset_runtime
      send("#{runtime_attr}=", send(runtime_attr) + runtime_after_render)
      runtime - runtime_after_render
    end

    define_method :append_info_to_payload do |payload|
      super(payload)
      runtime = (send(runtime_attr) || 0) + log_subscriber.reset_runtime
      payload[runtime_attr] = runtime
    end

    const_set(:ClassMethods, Module.new do
      define_method :log_process_action do |payload|
        messages = super(payload)
        runtime = payload[runtime_attr]
        if runtime && runtime != 0
          messages << format("#{name}: %.1fms", runtime.to_f)
        end
        messages
      end
    end)
  end
end