Sha256: 81b93a2f168967f1b12f8730145cf3c359ea2bb7914ab338708bae96ad4e1661

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 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|
            S.time_for(bin) { args.empty? ? self.send(without) : self.send(without, *args) }
          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|
            S.time_for(bin) { args.empty? ? send(without) : send(without, *args) }
          end
          self.singleton_class.class_eval { alias_method_chain target, feature }
        end
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fozzie-0.0.21 lib/fozzie/sniff.rb