Sha256: 80934c467878662b7bfdbc752ed09aa1bd69602fe68c3cdde1200ff87a15534f

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

# Calls <tt>Module#defined(file, line, method)</tt> after a class or module has been defined or redefined
module Defined
  autoload :Version, 'defined/version'

  class << self
    def included(mod) # :nodoc:
      set_trace_func method(:trace_function).to_proc
    end

    # An array of classes and modules that are currently being defined
    def definitions
      @definitions ||= []
    end

    protected

      def definition?(event, method, klass, keyword_event, method_event)
        event == keyword_event || (event == method_event && definition_method?(method, klass))
      end

      # Checks if the event is the end of a class or module definition
      def definition_end?(event, method, klass)
        !definitions.empty? && definition?(event, method, klass, 'end', 'c-return')
      end

      # Checks if the event is a call to <tt>Module.new</tt>, <tt>Class.new</tt>, or <tt>/(class|instance|module)_(eval|exec)/</tt>
      def definition_method?(method, klass)
        (method == :initialize && klass.is_a?(Module)) || method.to_s =~ /^(class|instance|module)_(eval|exec)$/
      end

      # Checks if the event is the start of a class or module definition
      def definition_start?(event, method, klass)
        definition?(event, method, klass, 'class', 'c-call')
      end

      # Checks if the event is the end of a class or module definition, and if so
      # it calls <tt>Module#defined(file, line, method)</tt> if implemented
      def trace_function(event, file, line, method, binding, klass)
        if definition_start?(event, method, klass)
          definitions << binding.eval('self')
        elsif definition_end?(event, method, klass)
          object = definitions.pop
          method ||= object.class.name.downcase.to_sym
          object.defined(file, line, method) if object.respond_to?(:defined)
        end
      end
  end
end

Module.send(:include, Defined)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
defined-0.0.0 lib/defined.rb