Module: DebugLogging::Util

Defined in:
lib/debug_logging/util.rb

Class Method Summary collapse

Class Method Details

.config_proxy_finder(scope:, method_name:, proxy_ref:, config_opts: {}, &block) ⇒ Object



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

def config_proxy_finder(scope:, method_name:, proxy_ref:, config_opts: {}, &block)
  if (proxy = scope.send(:instance_variable_get, DebugLogging::Configuration.config_pointer(proxy_ref,
                                                                                            method_name)))
    proxy
  else
    base = scope.respond_to?(:debug_config) ? scope.debug_config : DebugLogging.debug_logging_configuration
    proxy = if config_opts.empty?
              base
            else
              DebugLogging::Configuration.new(**base.to_hash.merge(config_opts))
            end
    proxy.register(method_name)
    scope.send(:instance_variable_set, DebugLogging::Configuration.config_pointer(proxy_ref, method_name),
               proxy)
    yield proxy if block
    proxy
  end
end

.extract_payload_and_config(method_names:, payload: nil, config: nil) ⇒ Object

methods_to_log may be an array of a single method name, followed by config options and payload,

or it could be an array of method names followed by config options and payload to be shared by the whole set.


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
# File 'lib/debug_logging/util.rb', line 7

def extract_payload_and_config(method_names:, payload: nil, config: nil)
  # When scoped config is present it will always be a new configuration instance per method
  # When scoped config is not present it will reuse the class' configuration object
  scoped_payload = (method_names.is_a?(Array) && method_names.last.is_a?(Hash) && method_names.pop.clone(freeze: false)) || {}
  payload = if payload
              payload.merge(scoped_payload)
            else
              scoped_payload
            end
  config_opts = config&.clone(freeze: false) || {}
  unless payload.empty?
    DebugLogging::Configuration::CONFIG_KEYS.each { |k| config_opts[k] = payload.delete(k) if payload.key?(k) }
  end
  method_names =
    case method_names
    when Symbol
      method_names
    when String
      method_names.to_sym
    when Array
      if method_names.first.is_a?(Array)
        # Array of arrays?
        method_names.shift
      elsif method_names.size == 1 && method_names.first.is_a?(Symbol)
        # when set as i_methods: [[:i_with_dsplat_payload, { tags: %w[blue green] }], ...]
        method_names.shift.to_sym
      else
        # Or an array of method name symbols?
        # logged :meth1, :meth2, :meth3 without options is valid
        method_names
      end
    end
  [method_names, payload, config_opts]
end

.payload_instance_vaiable_hydration(scope:, payload:) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/debug_logging/util.rb', line 42

def payload_instance_vaiable_hydration(scope:, payload:)
  paydirt = {}
  # TODO: Could make instance variable introspection configurable before or after method execution
  if payload.key?(:instance_variables)
    paydirt.merge!(payload.reject { |k| k == :instance_variables })
    payload[:instance_variables].each do |k|
      paydirt[k] = scope.send(:instance_variable_get, "@#{k}") if scope.send(:instance_variable_defined?, "@#{k}")
    end
  else
    paydirt.merge!(payload)
  end
  paydirt
end