<commit_before>require "method_decorators/version"

module MethodDecorators
  def method_added(name)
    super
    orig_method = instance_method(name)

    if    private_method_defined?(name);   visibility = :private
    elsif protected_method_defined?(name); visibility = :protected
    else                                   visibility = :public
    end

    decorators = MethodDecorator.current_decorators
    return  if decorators.empty?

    define_method(name) do |*args, &blk|
      decorated = MethodDecorators.decorate_callable(orig_method.bind(self), decorators)
      decorated.call(*args, &blk)
    end

    case visibility
    when :protected; protected name
    when :private;   private name
    end
  end

  def singleton_method_added(name)
    super
    orig_method = method(name)

    decorators = MethodDecorator.current_decorators
    return  if decorators.empty?

    MethodDecorators.define_others_singleton_method(self, name) do |*args, &blk|
      decorated = MethodDecorators.decorate_callable(orig_method, decorators)
      decorated.call(*args, &blk)
    end
  end

  def self.decorate_callable(callable, decorators)
    decorators.reduce(callable) do |callable, decorator|
      lambda{|*a, &b| decorator.call(callable, *a, &b) }
    end
  end

  def self.define_others_singleton_method(klass, name, &blk)
    if klass.respond_to?(:define_singleton_method)
      klass.define_singleton_method(name, &blk)
    else
      class << klass
        self
      end.send(:define_method, name, &blk)
    end
  end
end

class MethodDecorator
  @@current_decorators = []

  def self.current_decorators
    decs = @@current_decorators
    @@current_decorators = []
    decs
  end

  def self.+@
    +new
  end<commit_msg>Style<commit_after>require "method_decorators/version"

module MethodDecorators
  def method_added(name)
    super
    orig_method = instance_method(name)

    if    private_method_defined?(name);   visibility = :private
    elsif protected_method_defined?(name); visibility = :protected
    else                                   visibility = :public
    end

    decorators = MethodDecorator.current_decorators
    return  if decorators.empty?

    define_method(name) do |*args, &blk|
      decorated = MethodDecorators.decorate_callable(orig_method.bind(self), decorators)
      decorated.call(*args, &blk)
    end

    case visibility
    when :protected; protected name
    when :private;   private name
    end
  end

  def singleton_method_added(name)
    super
    orig_method = method(name)

    decorators = MethodDecorator.current_decorators
    return  if decorators.empty?

    MethodDecorators.define_others_singleton_method(self, name) do |*args, &blk|
      decorated = MethodDecorators.decorate_callable(orig_method, decorators)
      decorated.call(*args, &blk)
    end
  end

  def self.decorate_callable(callable, decorators)
    decorators.reduce(callable) do |callable, decorator|
      lambda{ |*a, &b| decorator.call(callable, *a, &b) }
    end
  end

  def self.define_others_singleton_method(klass, name, &blk)
    if klass.respond_to?(:define_singleton_method)
      klass.define_singleton_method(name, &blk)
    else
      class << klass
        self
      end.send(:define_method, name, &blk)
    end
  end
end

class MethodDecorator
  @@current_decorators = []

  def self.current_decorators
    decs = @@current_decorators
    @@current_decorators = []
    decs
  end

  def self.+@
    +new
  end
{'fraction_non_alphanumeric': 0.08658008658008658, 'fraction_numerical': 0.0, 'mean_word_length': 3.268472906403941, 'pattern_counts': {'":': 0, '<': 7, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5182998', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1087, 'n_words': 232}