Sha256: 8f508030a75b38ad8d4bb9487cf3bd298ec595d627ffc5d7373ef287a9a1f577

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

module Warp
  class Instrument
    class << self
      def for(klass, method)
        @@registry ||= {}
        @@registry[klass] ||= {}
        @@registry[klass][method] || new(klass, method)
      end

      private

      def register(instrument)
        @@registry ||= {}
        @@registry[instrument.klass] ||= {}
        @@registry[instrument.klass][instrument.method] = instrument
      end
    end

    attr_accessor :klass, :method, :enabled, :calls

    def initialize(klass, method)
      self.klass = klass
      self.method = method
      self.enabled = false
      self.calls = []
      setup_hook

      self.class.send(:register, self)
    end

    def reset
      self.calls = []
    end

    def run
      enable
      yield
      disable
    end

    def enabled?
      enabled
    end

    def log(args)
      calls << args if enabled?
    end

    private

    def enable
      self.enabled = true
    end

    def disable
      self.enabled = false
    end

    def setup_hook
      original_method = klass.instance_method(method)

      klass.send(:define_method, method) do |*args|
        result = original_method.bind(self).call(*args)

        Warp::Instrument.for(self.class, __method__).log([args, result])

        result
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
warp-1.5.0 lib/warp/instrument.rb
warp-1.4.0 lib/warp/instrument.rb