Sha256: ad317a0697e2658bf1b3e3a3094b397e9fe89d154cc22f2876be05b930405327

Contents?: true

Size: 1.63 KB

Versions: 6

Compression:

Stored size: 1.63 KB

Contents

require 'core_ext/module/monitor'
require 'facets/module/alias_method_chain' unless Module.methods.include?(:alias_method_chain)
require 'facets/string/snakecase'

module Fozzie
  module Sniff

    def self.included(klass)
      return if klass.include?(ClassMethods)

      klass.class_eval { extend ClassMethods }
    end

    module ClassMethods

      def _monitor
        @_monitor_flag = true
      end

      def _monitor_meth(target, &blk)
        return if @_monitor_flag.nil? || !@_monitor_flag

        @_monitor_flag, feature, bin = false, :monitor, [self.name.snakecase, target.to_s.snakecase]
        aliased_target, punctuation  = target.to_s.sub(/([?!=])$/, ''), $1

        with    = "#{aliased_target}_with_#{feature}#{punctuation}"
        without = "#{aliased_target}_without_#{feature}#{punctuation}"

        blk.call(with, without, feature, bin)
      end

      def method_added(target)
        _monitor_meth(target) do |with, without, feature, bin|
          define_method(with) do |*args, &blk|
            S.time_for(bin) do
              args.empty? ? self.send(without, &blk) : self.send(without, *args, &blk)
            end
          end
          self.alias_method_chain(target, feature)
        end
      end

      def singleton_method_added(target)
        _monitor_meth(target) do |with, without, feature, bin|
          define_singleton_method(with) do |*args, &blk|
            S.time_for(bin) do
              args.empty? ? send(without, &blk) : send(without, *args, &blk)
            end
          end
          self.singleton_class.class_eval { alias_method_chain target, feature }
        end
      end

    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fozzie-0.0.27 lib/fozzie/sniff.rb
fozzie-0.0.26 lib/fozzie/sniff.rb
fozzie-0.0.25 lib/fozzie/sniff.rb
fozzie-0.0.24 lib/fozzie/sniff.rb
fozzie-0.0.23 lib/fozzie/sniff.rb
fozzie-0.0.22 lib/fozzie/sniff.rb