Sha256: 1fcf449f87818ff83dbb9d0c36554769c3d167660c886fce7a76c6a1f2d1cc59

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

require_relative './method_instrumentor'

module InstrumentAllTheThings
  module MethodProxy
    def self.for_class(klass)
      find_for_class(klass) || install_on_class(klass)
    end

    def self.find_for_class(klass)
      klass.ancestors.detect do |a|
        a.is_a?(Instrumentor) &&
          a._iatt_built_for == klass
      end
    end

    def self.install_on_class(klass)
      construct_for_class(klass).tap do |m|
        klass.prepend(m)
      end
    end

    def self.construct_for_class(klass)
      Module.new do
        extend Instrumentor
      end.tap { |m| m._iatt_built_for = klass }
    end

    module Instrumentor
      def inspect
        "InstrumentAllTheThings::#{@_iatt_built_for}Proxy"
      end

      def _iatt_built_for
        @_iatt_built_for
      end

      def _iatt_built_for=(val)
        @_iatt_built_for = val
      end

      def wrap_implementation(method_name, settings)
        wrap = MethodInstrumentor.new(**settings)

        define_method(method_name) do |*args, **kwargs, &blk|
          wrap.invoke(klass: is_a?(Class) ? self : self.class) do
            super(*args, **kwargs, &blk)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
instrument_all_the_things-1.0.2 lib/instrument_all_the_things/method_proxy.rb
instrument_all_the_things-1.0.1 lib/instrument_all_the_things/method_proxy.rb
instrument_all_the_things-0.9.1.alpha lib/instrument_all_the_things/method_proxy.rb
instrument_all_the_things-0.9.0.alpha lib/instrument_all_the_things/method_proxy.rb