<commit_before>module Autoextend
  Extension = Struct.new(:module_name, :method, :block) do
    def extend(klass)
      if block
        block.call(klass)
      else
        klass.send(method, Autoextend.const_get(module_name.to_s))
      end
    end
  end

  def self.extensions
    @extensions ||= Hash.new
  end

  MethodExtension = Struct.new(:target, :feature) do
    # based off ActiveSupport's alias_method_chain
    def extend(klass)
      return if klass.instance_variable_get(:@autoextending)
      begin
        klass.instance_variable_set(:@autoextending, true)

        # Strip out punctuation on predicates, bang or writer methods since
        # e.g. target?_without_feature is not a valid method name.
        aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1

        with_method = "#{aliased_target}_with_#{feature}#{punctuation}"
        without_method = "#{aliased_target}_without_#{feature}#{punctuation}"

        # make sure we're not inserting ourselves multiples times (could be
        # caused by someone else doing an alias_method_chain)
        return if klass.method_defined?(without_method)

        klass.send(:alias_method, without_method, target)
        klass.send(:alias_method, target, with_method)

        case
          when klass.public_method_defined?(without_method)
            klass.send(:public, target)
          when klass.protected_method_defined?(without_method)
            klass.send(:protected, target)
          when klass.private_method_defined?(without_method)
            klass.send(:private, target)
        end
      ensure
        klass.instance_variable_set(:@autoextending, false)
      end
    end
  end

  def self.included(klass)
    klass.extend(ClassMethods)
  end

  def self.const_defined?(class_name)
    if RUBY_VERSION >= '2.0.0'
      Object.const_defined?(class_name, false)
    else
      class_name.to_s.split("::").inject(Object) do |parent, name|
        return false unless parent.const_defined?(name, false)
        parent.const_get(name, false)
      end
    end
  end

  def self.const_get(class_name)
    if RUBY_VERSION >= '2.0.0'
      Object.const_get(class_name, false)
    else
      class_name.to_s.split("::").inject(Object) { |parent, name| parent.const_get(name, false) }
    end
  end

  module ClassMethods
    def singleton_method_added(method)
      singleton_autoextensions.fetch(method, []).each do |extension|
        extension.extend(self.singleton_class)
      end
      super
    end

    def method_added(method)
      autoextensions.fetch(method, []).each do |extension|
        extension.extend(self)
      end
      super
    end

    def autoextensions
      @autoextensions ||= Hash.new
    end

    def singleton_autoextensions
      @autoextensions ||= Hash.new
    end

    def autoextend_singleton(method, feature)
      extension = autoextend_method(method, feature, singleton_autoextensions)
      if singleton_class.method_defined?(method)
        extension.extend(singleton_class)
      end
    end

    def autoextend(method, feature)
      extension = autoextend_method(method, feature, autoextensions)
      if method_defined?(method)
        extension.extend(self)
      end
    end<commit_msg>don't autoextend methods from modules<commit_after>module Autoextend
  Extension = Struct.new(:module_name, :method, :block) do
    def extend(klass)
      if block
        block.call(klass)
      else
        klass.send(method, Autoextend.const_get(module_name.to_s))
      end
    end
  end

  def self.extensions
    @extensions ||= Hash.new
  end

  MethodExtension = Struct.new(:target, :feature) do
    # based off ActiveSupport's alias_method_chain
    def extend(klass)
      return if klass.instance_variable_get(:@autoextending)
      begin
        klass.instance_variable_set(:@autoextending, true)

        # Strip out punctuation on predicates, bang or writer methods since
        # e.g. target?_without_feature is not a valid method name.
        aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1

        with_method = "#{aliased_target}_with_#{feature}#{punctuation}"
        without_method = "#{aliased_target}_without_#{feature}#{punctuation}"

        # make sure we're not inserting ourselves multiples times (could be
        # caused by someone else doing an alias_method_chain)
        return if klass.method_defined?(without_method)

        klass.send(:alias_method, without_method, target)
        klass.send(:alias_method, target, with_method)

        case
          when klass.public_method_defined?(without_method)
            klass.send(:public, target)
          when klass.protected_method_defined?(without_method)
            klass.send(:protected, target)
          when klass.private_method_defined?(without_method)
            klass.send(:private, target)
        end
      ensure
        klass.instance_variable_set(:@autoextending, false)
      end
    end
  end

  def self.included(klass)
    klass.extend(ClassMethods)
  end

  def self.const_defined?(class_name)
    if RUBY_VERSION >= '2.0.0'
      Object.const_defined?(class_name, false)
    else
      class_name.to_s.split("::").inject(Object) do |parent, name|
        return false unless parent.const_defined?(name, false)
        parent.const_get(name, false)
      end
    end
  end

  def self.const_get(class_name)
    if RUBY_VERSION >= '2.0.0'
      Object.const_get(class_name, false)
    else
      class_name.to_s.split("::").inject(Object) { |parent, name| parent.const_get(name, false) }
    end
  end

  module ClassMethods
    def singleton_method_added(method)
      singleton_autoextensions.fetch(method, []).each do |extension|
        extension.extend(self.singleton_class)
      end
      super
    end

    def method_added(method)
      autoextensions.fetch(method, []).each do |extension|
        extension.extend(self)
      end
      super
    end

    def autoextensions
      @autoextensions ||= Hash.new
    end

    def singleton_autoextensions
      @autoextensions ||= Hash.new
    end

    def autoextend_singleton(method, feature)
      extension = autoextend_method(method, feature, singleton_autoextensions)
      if singleton_class.method_defined?(method) && method(method).owner == singleton_class
        extension.extend(singleton_class)
      end
    end

    def autoextend(method, feature)
      extension = autoextend_method(method, feature, autoextensions)
      if method_defined?(method) && instance_method(method).owner == self
        extension.extend(self)
      end
    end
{'fraction_non_alphanumeric': 0.09485383678440926, 'fraction_numerical': 0.002131546894031669, 'mean_word_length': 2.9453453453453453, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4990600', 'n_tokens_mistral': 1943, 'n_tokens_neox': 1952, 'n_words': 423}