<commit_before>module TrafficJam
  class LimitGroup
    attr_reader :limits

    def initialize(*limits)
      @limits = limits.flatten
    end

    def <<(limit)
      limits << limit
    end

    def increment(amount = 1, time: Time.now)
      exceeded_index = limits.find_index do |limit|
        !limit.increment(amount, time: time)
      end
      if exceeded_index
        limits[0...exceeded_index].each do |limit|
          limit.decrement(amount, time: time)
        end
      end
      exceeded_index.nil?
    end

    def increment!(amount = 1, time: Time.now)
      exceeded_index = limits.find_index do |limit|
        !limit.increment(amount, time: time)
      end
      if exceeded_index
        limits[0...exceeded_index].each do |limit|
          limit.decrement(amount, time: time)
        end
        raise TrafficJam::LimitExceededError.new(limits[exceeded_index])
      elsif block_given?
        result =
          begin
            yield
          rescue => e
            decrement(amount, time: time)
            raise e
          end
        decrement(amount, time: time) if result == false
        result
      end
    end

    def decrement(amount = 1, time: Time.now)
      limits.all? { |limit| limit.decrement(amount, time: time) }
    end

    def exceeded?(amount = 1)
      limits.any? { |limit| limit.exceeded?(amount) }
    end

    def limit_exceeded(amount = 1)
      limits.find { |limit| limit.limit_exceeded(amount) }
    end

    def reset
      limits.each(&:reset)
      nil
    end

    def remaining
      limits.map(&:remaining).min
    end
  end
end
<commit_msg>Fix LimitGroup#limit_exceeded.<commit_after>module TrafficJam
  class LimitGroup
    attr_reader :limits

    def initialize(*limits)
      @limits = limits.flatten
    end

    def <<(limit)
      limits << limit
    end

    def increment(amount = 1, time: Time.now)
      exceeded_index = limits.find_index do |limit|
        !limit.increment(amount, time: time)
      end
      if exceeded_index
        limits[0...exceeded_index].each do |limit|
          limit.decrement(amount, time: time)
        end
      end
      exceeded_index.nil?
    end

    def increment!(amount = 1, time: Time.now)
      exceeded_index = limits.find_index do |limit|
        !limit.increment(amount, time: time)
      end
      if exceeded_index
        limits[0...exceeded_index].each do |limit|
          limit.decrement(amount, time: time)
        end
        raise TrafficJam::LimitExceededError.new(limits[exceeded_index])
      elsif block_given?
        result =
          begin
            yield
          rescue => e
            decrement(amount, time: time)
            raise e
          end
        decrement(amount, time: time) if result == false
        result
      end
    end

    def decrement(amount = 1, time: Time.now)
      limits.all? { |limit| limit.decrement(amount, time: time) }
    end

    def exceeded?(amount = 1)
      limits.any? { |limit| limit.exceeded?(amount) }
    end

    def limit_exceeded(amount = 1)
      limits.each do |limit|
        limit_exceeded = limit.limit_exceeded(amount)
        return limit_exceeded if limit_exceeded
      end
      nil
    end

    def reset
      limits.each(&:reset)
      nil
    end

    def remaining
      limits.map(&:remaining).min
    end
  end
end

{'fraction_non_alphanumeric': 0.09107303877366997, 'fraction_numerical': 0.004207995190862639, 'mean_word_length': 2.0144927536231885, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '3720509', 'n_tokens_mistral': 1051, 'n_tokens_neox': 991, 'n_words': 255}