<commit_before>require "hijack_method/version"

module HijackMethod

  def hijack_method(method_name, options={})
    
    method_name = method_name.to_sym
    hijacked_method_name = :"___hijacked_#{method_name}" 
    
    return if instance_methods(false).include? hijacked_method_name 

    alias_method hijacked_method_name, method_name

    define_method(method_name) do |*args, &block|
      options[:before].call(*args, &block) unless options[:before].nil?
      ret_value = send(hijacked_method_name, *args, &block)
      options[:after].call(*args, &block) unless options[:after].nil?
      ret_value
    end
  end

end<commit_msg>Refactors hijack_method into class Hijacker<commit_after>require "hijack_method/version"

module HijackMethod

  class Hijacker < Struct.new(:object)

    def hijack(method_name, options={})

      method_name = method_name.to_sym
      hijacked_method_name = generate_name(method_name)

      return if object.instance_methods(false).include? hijacked_method_name 

      object.send(:alias_method, hijacked_method_name, method_name)
      new_method = Proc.new do |*args, &block|
        options[:before].call(*args, &block) unless options[:before].nil?
        ret_value = options[:main].nil? ? 
          send(hijacked_method_name, *args, &block) :
          options[:main].call(*args, &block)
        options[:after].call(*args, &block) unless options[:after].nil?
        ret_value
      end
      object.send(:define_method, method_name, new_method)
    end

    private
    
    def generate_name(method_name)
      :"___hijacked_#{method_name}"
    end
  end

  def hijack_method(method_name, options={})
    Hijacker.new(self).hijack(method_name, options)
  end

end
{'fraction_non_alphanumeric': 0.10845481049562682, 'fraction_numerical': 0.0, 'mean_word_length': 3.888888888888889, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, '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': '6396843', 'n_tokens_mistral': 557, 'n_tokens_neox': 548, 'n_words': 101}