class Datadog::Shim

A “stand-in” that intercepts calls to another object. i.e. man-in-the-middle. This shim forwards all methods to object, except those overriden. Useful if you want to intercept inbound behavior to an object without modifying the object in question, especially useful if the overridding behavior shouldn't be global.

Constants

EXCLUDED_METHODS
METHODS

Attributes

shim[R]
shim_target[R]

Public Class Methods

new(shim_target) { |self| ... } click to toggle source

Pass this a block to override methods

# File lib/ddtrace/augmentation/shim.rb, line 46
def initialize(shim_target)
  @shim = self
  @shim_target = shim_target

  # Save a reference to the original :define_singleton_method
  # so methods can be defined on the shim after forwarding is applied.
  @definition_method = method(:define_singleton_method)

  # Wrap any methods
  yield(self) if block_given?

  # Forward methods
  forwarded_methods = (
    shim_target.public_methods.to_set \
    - METHODS \
    - EXCLUDED_METHODS \
    - wrapped_methods
  )
  forward_methods!(*forwarded_methods)
end
shim?(object) click to toggle source
# File lib/ddtrace/augmentation/shim.rb, line 38
def self.shim?(object)
  # Check whether it responds to #shim? because otherwise the
  # Shim forwards all method calls, including type checks to
  # the wrapped object, to mimimize its intrusion.
  object.respond_to?(:shim?)
end

Public Instance Methods

override_method!(method_name, &block) click to toggle source
# File lib/ddtrace/augmentation/shim.rb, line 67
def override_method!(method_name, &block)
  return unless block_given?

  without_warnings do
    @definition_method.call(method_name, &block).tap do
      wrapped_methods.add(method_name)
    end
  end
end
respond_to?(method_name) click to toggle source
# File lib/ddtrace/augmentation/shim.rb, line 85
def respond_to?(method_name)
  return true if METHODS.include?(method_name)
  shim_target.respond_to?(method_name)
end
shim?() click to toggle source
# File lib/ddtrace/augmentation/shim.rb, line 81
def shim?
  true
end
wrap_method!(method_name, &block) click to toggle source
Calls superclass method Datadog::MethodWrapping#wrap_method!
# File lib/ddtrace/augmentation/shim.rb, line 77
def wrap_method!(method_name, &block)
  super(shim_target.method(method_name), &block)
end

Private Instance Methods

forward_methods!(*forwarded_methods) click to toggle source
# File lib/ddtrace/augmentation/shim.rb, line 92
def forward_methods!(*forwarded_methods)
  return if forwarded_methods.empty?

  singleton_class.send(
    :def_delegators,
    :@shim_target,
    *forwarded_methods
  )
end