Sha256: 85bcd026e97ed370cbc5642c90fcef19aee45aa46c7e3c590d288098bb1044af

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

class Remnant
  module Discover
    module ClassMethods
      def find(key, klass, method, instance = true)
        rediscover(key, klass, method, instance) if ActiveSupport::Dependencies.will_unload?(klass)

        klass.class_eval <<-EOL, __FILE__, __LINE__
          #{"class << self" unless instance}
          def #{method}_with_remnant(*args, &block)
            ::Remnant::Discover.measure(#{key.inspect}) do
              #{method}_without_remnant(*args, &block)
            end
          end

          alias_method_chain :#{method}, :remnant
          #{"end" unless instance}
        EOL
      end

      def measure(key, &block)
        if Remnant::Discover.running.include?(key)
          yield
        else
          result = nil
          Remnant::Discover.running << key
          begin
            Remnant::Discover.results[key] += Benchmark.ms { result = yield }
          rescue
            raise
          ensure
            Remnant::Discover.running.delete(key)
          end
          result
        end
      end

      def results
        Thread.current[:result] ||= Hash.new(0)
      end

      def running
        Thread.current[:running] ||= []
      end

      def remnants_to_rediscover
        @remnants_to_rediscover ||= []
      end

      def rediscover(*args)
        remnants_to_rediscover << args unless remnants_to_rediscover.include?(args)
      end

      def rediscover!
        remnants_to_rediscover.map do |(key, klass_name, method, instance)|
          find(key, klass_name.constantize, method, instance)
        end
      end
    end
    extend ClassMethods
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
remnant-0.2.0 lib/remnant/discover.rb
remnant-0.1.3 lib/remnant/discover.rb
remnant-0.1.2 lib/remnant/discover.rb
remnant-0.1.1 lib/remnant/discover.rb
remnant-0.1.0 lib/remnant/discover.rb