Module: TraceViz::Helpers::TracePoint::ParamHelper

Included in:
TraceData::TracePoint::MethodCall
Defined in:
lib/trace_viz/helpers/trace_point/param_helper.rb

Constant Summary collapse

IGNORED_VARIABLES =
[
  "__ENCODING__", "__LINE__", "__FILE__",
].freeze
IGNORED_METHODS =
[
  "initialize",
  "inspect",
  "object_id",
  "to_s",
  "class",
  "method",
  "instance_variable_get",
  "instance_variable_set",
  "instance_variables",
  "public_methods",
  "private_methods",
  "protected_methods",
  "respond_to?",
  "send",
].freeze

Instance Method Summary collapse

Instance Method Details

#extract_parameters_from_signature(parameters, binding) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/trace_viz/helpers/trace_point/param_helper.rb', line 46

def extract_parameters_from_signature(parameters, binding)
  parameters.each_with_object({}) do |(_, name), hash|
    next unless name
    next if should_ignore_parameter?(name)

    hash[name] = safe_local_variable_get(binding, name)
  end
end

#extract_params(binding, method_name) ⇒ Object



28
29
30
31
32
33
# File 'lib/trace_viz/helpers/trace_point/param_helper.rb', line 28

def extract_params(binding, method_name)
  method = fetch_method_via_instance_method(binding, method_name)
  return {} unless method

  extract_parameters_from_signature(method.parameters, binding)
end

#fetch_method_via_instance_method(binding, method_name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/trace_viz/helpers/trace_point/param_helper.rb', line 35

def fetch_method_via_instance_method(binding, method_name)
  klass_instance = safe_eval(binding, "self")
  return unless klass_instance

  klass = klass_instance.class
  klass.instance_method(method_name)
rescue StandardError
  # TraceViz.logger.error("Failed to fetch method #{method_name}: #{e.message}")
  nil
end

#placeholder_parameter?(parameter_name_str) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/trace_viz/helpers/trace_point/param_helper.rb', line 92

def placeholder_parameter?(parameter_name_str)
  parameter_name_str.start_with?("_")
end

#safe_eval(binding, expression) ⇒ Object



63
64
65
66
67
68
# File 'lib/trace_viz/helpers/trace_point/param_helper.rb', line 63

def safe_eval(binding, expression)
  binding.eval(expression)
rescue StandardError => e
  TraceViz.logger.error("Failed to eval expression #{expression}: #{e.message}")
  nil
end

#safe_local_variable_get(binding, name) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/trace_viz/helpers/trace_point/param_helper.rb', line 55

def safe_local_variable_get(binding, name)
  binding.local_variable_get(name)
rescue NameError
  # class_name = safe_eval(binding, "self.class") || "unknown class"
  # TraceViz.logger.error("Failed to get local variable '#{name}' in #{class_name}: #{e.message}")
  nil
end

#should_ignore_method?(method_name_str) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/trace_viz/helpers/trace_point/param_helper.rb', line 84

def should_ignore_method?(method_name_str)
  IGNORED_METHODS.include?(method_name_str)
end

#should_ignore_parameter?(parameter_name) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
# File 'lib/trace_viz/helpers/trace_point/param_helper.rb', line 70

def should_ignore_parameter?(parameter_name)
  parameter_name_str = parameter_name.to_s
  return true if should_ignore_variable?(parameter_name_str)
  return true if should_ignore_method?(parameter_name_str)
  return true if splat_parameter?(parameter_name_str)
  return true if placeholder_parameter?(parameter_name_str)

  false
end

#should_ignore_variable?(variable_name_str) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/trace_viz/helpers/trace_point/param_helper.rb', line 80

def should_ignore_variable?(variable_name_str)
  IGNORED_VARIABLES.include?(variable_name_str)
end

#splat_parameter?(parameter_name_str) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/trace_viz/helpers/trace_point/param_helper.rb', line 88

def splat_parameter?(parameter_name_str)
  parameter_name_str.include?("*")
end