<commit_before>module Bullet
  module Detector
    class Counter < Base
      def self.clear
        @@possible_objects = nil
        @@impossible_objects = nil
      end

      def self.add_counter_cache(object, associations)
        if conditions_met?( object, associations )
          create_notification object.class, associations
        end
      end

      def self.add_possible_objects(objects)
        klazz = objects.first.class
        possible_objects[klazz] ||= []
        possible_objects[klazz] << objects
        unique(possible_objects[klazz])
      end

      def self.add_impossible_object(object)
        klazz = object.class
        impossible_objects[klazz] ||= []
        impossible_objects[klazz] << object
        impossible_objects[klazz].uniq!
      end
      
      private
      def self.create_notification( klazz, associations )
         notice = Bullet::Notification::CounterCache.new klazz, associations
         Bullet.add_notification notice
      end

      def self.possible_objects
        @@possible_objects ||= {}
      end

      def self.impossible_objects
        @@impossible_objects ||= {}
      end

      def self.conditions_met?( object, associations )
        object_in_possible_objects?( object ) and
        object_not_in_impossible_objects?( object )
      end

      def self.object_in_possible_objects?( object )
        !possible_objects[ object.class ].nil? and 
        possible_objects[ object.class ].include?( object )
      end

      def self.object_not_in_impossible_objects?( object )
        impossible_objects[ object.class ].nil? or
        !impossible_objects[ object.class ].include?( object )
      end
    end
  end
end
<commit_msg>Use ObjectRegistry in Detector::Counter<commit_after>module Bullet
  module Detector
    class Counter < Base
      def self.clear
        @@possible_objects = nil
        @@impossible_objects = nil
      end

      def self.add_counter_cache(object, associations)
        if conditions_met?( object, associations )
          create_notification object.class, associations
        end
      end

      def self.add_possible_objects(objects)
        possible_objects.add objects
      end

      def self.add_impossible_object(object)
        impossible_objects.add object
      end
      
      private
      def self.create_notification( klazz, associations )
         notice = Bullet::Notification::CounterCache.new klazz, associations
         Bullet.add_notification notice
      end

      def self.possible_objects
        @@possible_objects ||= Bullet::ObjectRegistry.new
      end

      def self.impossible_objects
        @@impossible_objects ||= Bullet::ObjectRegistry.new
      end

      def self.conditions_met?( object, associations )
        possible_objects.contains?( object ) and
        !impossible_objects.contains?( object )
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.0729673384294649, 'fraction_numerical': 0.0, 'mean_word_length': 2.4645006016847173, 'pattern_counts': {'":': 0, '<': 9, '<?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': '4487572', 'n_tokens_mistral': 775, 'n_tokens_neox': 760, 'n_words': 175}