File: chain_proxy.rb

package info (click to toggle)
ruby-naught 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 180 kB
  • sloc: ruby: 658; makefile: 6
file content (51 lines) | stat: -rw-r--r-- 1,693 bytes parent folder | download
1
2
3
4
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
require "naught/basic_object"

module Naught
  # Lightweight proxy for tracking chained method calls
  #
  # Used by the callstack feature to group chained method calls
  # (e.g., `null.foo.bar.baz`) into a single trace while keeping
  # separate calls (e.g., `null.foo; null.bar`) in separate traces.
  #
  # @api private
  class ChainProxy < BasicObject
    # Create a new ChainProxy
    #
    # @param root [Object] the original null object being tracked
    # @param current_trace [Array<CallLocation>] the trace to append calls to
    def initialize(root, current_trace)
      @root = root
      @current_trace = current_trace
    end

    # Handle method calls by recording them and returning self for chaining
    #
    # @param method_name [Symbol] the method being called
    # @param args [Array] arguments passed to the method
    # @return [ChainProxy] self for method chaining
    # rubocop:disable Style/MissingRespondToMissing -- BasicObject doesn't use respond_to_missing?
    def method_missing(method_name, *args)
      location = ::Naught::CallLocation.from_caller(
        method_name, args, ::Kernel.caller(1, 1).first
      )
      @current_trace << location
      self
    end
    # rubocop:enable Style/MissingRespondToMissing

    # Check if the proxy responds to a method
    #
    # @return [true] chain proxies respond to any method
    def respond_to?(*, **) = true

    # Return a string representation of the proxy
    #
    # @return [String] a simple representation of the proxy
    def inspect = "<null:chain>"

    # Return the class of the root null object
    #
    # @return [Class] the class of the root null object
    def class = @root.class
  end
end