Class: Naught::CallLocation

Inherits:
Object
  • Object
show all
Defined in:
lib/naught/call_location.rb

Overview

Represents a single method call in a null object's call trace

This class provides an interface similar to Thread::Backtrace::Location, capturing information about where a method was called on a null object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, args:, path:, lineno:, base_label: nil) ⇒ CallLocation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new CallLocation

Parameters:

  • label (Symbol, String)

    the method that was called

  • args (Array<Object>)

    arguments passed to the method

  • path (String)

    path to the file where the call originated

  • lineno (Integer)

    line number where the call originated

  • base_label (String, nil) (defaults to: nil)

    name of the method that made the call



77
78
79
80
81
82
83
# File 'lib/naught/call_location.rb', line 77

def initialize(label:, args:, path:, lineno:, base_label: nil)
  @label = label.to_s
  @args = args.dup.freeze
  @path = path
  @lineno = lineno
  @base_label = base_label
end

Instance Attribute Details

#argsArray<Object> (readonly)

Arguments passed to the method call

Examples:

location.args #=> [1, 2, 3]

Returns:

  • (Array<Object>)

    arguments passed to the method call



39
40
41
# File 'lib/naught/call_location.rb', line 39

def args
  @args
end

#base_labelString? (readonly)

The name of the method that made the call

Examples:

location.base_label #=> "some_method"

Returns:

  • (String, nil)

    the name of the method that made the call



67
68
69
# File 'lib/naught/call_location.rb', line 67

def base_label
  @base_label
end

#labelString (readonly)

The name of the method that was called

Examples:

location.label #=> "foo"

Returns:

  • (String)

    the name of the method that was called



32
33
34
# File 'lib/naught/call_location.rb', line 32

def label
  @label
end

#linenoInteger (readonly)

The line number where the call originated

Examples:

location.lineno #=> 42

Returns:

  • (Integer)

    the line number where the call originated



60
61
62
# File 'lib/naught/call_location.rb', line 60

def lineno
  @lineno
end

#pathString (readonly) Also known as: absolute_path

The absolute path to the file where the call originated

Examples:

location.path #=> "/path/to/file.rb"

Returns:

  • (String)

    the absolute path to the file where the call originated



46
47
48
# File 'lib/naught/call_location.rb', line 46

def path
  @path
end

Class Method Details

.from_caller(method_name, args, caller_string) ⇒ CallLocation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a CallLocation from a caller string

Parameters:

  • method_name (Symbol, String)

    the method that was called

  • args (Array<Object>)

    arguments passed to the method

  • caller_string (String, nil)

    a single entry from Kernel.caller

Returns:



16
17
18
19
20
21
22
23
24
25
# File 'lib/naught/call_location.rb', line 16

def self.from_caller(method_name, args, caller_string)
  data = CallerInfo.parse(caller_string || "")
  new(
    label: method_name,
    args: args,
    path: data[:path] || "",
    lineno: data[:lineno],
    base_label: data[:base_label]
  )
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare this CallLocation with another for equality

Examples:

location1 == location2 #=> true

Parameters:

Returns:

  • (Boolean)

    true if all attributes match



109
110
111
112
113
114
115
116
# File 'lib/naught/call_location.rb', line 109

def ==(other)
  other.is_a?(CallLocation) &&
    label == other.label &&
    args == other.args &&
    path == other.path &&
    lineno == other.lineno &&
    base_label == other.base_label
end

#hashInteger

Compute a hash value for this CallLocation

Examples:

location.hash #=> 123456789

Returns:

  • (Integer)

    hash value based on all attributes



129
# File 'lib/naught/call_location.rb', line 129

def hash = [label, args, path, lineno, base_label].hash

#inspectString

Returns a detailed inspect representation

Examples:

location.inspect #=> "#<Naught::CallLocation /path/to/file.rb:42 -> foo(1)>"

Returns:

  • (String)

    inspect representation



101
# File 'lib/naught/call_location.rb', line 101

def inspect = "#<#{self.class} #{self}>"

#to_sString

Returns a human-readable string representation of the call

Examples:

location.to_s #=> "/path/to/file.rb:42:in `method' -> foo(1, 2)"

Returns:

  • (String)

    string representation



90
91
92
93
94
# File 'lib/naught/call_location.rb', line 90

def to_s
  pretty_args = args.map(&:inspect).join(", ")
  location = base_label ? "#{path}:#{lineno}:in `#{base_label}'" : "#{path}:#{lineno}"
  "#{location} -> #{label}(#{pretty_args})"
end