<commit_before>module DataMapper
  # CounterCacheable allows you to transparently maintain counts on collection association of this model on the parent model.
  # You can also specify a custom counter cache column by providing a column name instead of a true/false value 
  # to this option (e.g., :counter_cache => :my_custom_counter.)
  module CounterCacheable

    def self.included(klass)      
      DataMapper::Associations::ManyToOne.module_eval {
        extend DataMapper::CounterCacheable::ClassMethods
        
        (class << self; self; end).class_eval do
          unless method_defined?(:setup_without_counter_caching)
            alias_method :setup_without_counter_caching, :setup 
            alias_method :setup, :setup_with_counter_caching
          end
        end

      }
    end

    module ClassMethods
      
      def setup_with_counter_caching(name, model, options = {})
        counter_cache_attribute = options.delete(:counter_cache)
        relationship = setup_without_counter_caching(name, model, options)
        
        if counter_cache_attribute
          case counter_cache_attribute
            when String, Symbol
              counter_cache_attribute = counter_cache_attribute.to_s
            else
              counter_cache_attribute = "#{model.storage_name}_count".to_s
          end

          relationship.parent_model.class_eval <<-EOS, __FILE__, __LINE__
            property :#{counter_cache_attribute}, Integer, :default => 0, :lazy => false          
          EOS

          model.class_eval <<-EOS, __FILE__, __LINE__            
            after :create, :increment_counter_cache_for_#{name}
            after :destroy, :decrement_counter_cache_for_#{name}
          
            def increment_counter_cache_for_#{name}
              if self.class == #{model.name}
                self.#{name}.update_attributes(:#{counter_cache_attribute} => self.#{name}.reload.#{counter_cache_attribute}.succ)
              end
            end

            def decrement_counter_cache_for_#{name}
              if self.class == #{model.name}
                self.#{name}.update_attributes(:#{counter_cache_attribute} => self.#{name}.reload.#{counter_cache_attribute} - 1)
              end
            end
            
          EOS
        end

        relationship
      end

    end # ClassMethods<commit_msg>Check the counter property doesn't already exist on the parent model<commit_after>module DataMapper
  # CounterCacheable allows you to transparently maintain counts on collection association of this model on the parent model.
  # You can also specify a custom counter cache column by providing a column name instead of a true/false value 
  # to this option (e.g., :counter_cache => :my_custom_counter.)
  module CounterCacheable

    def self.included(klass)      
      DataMapper::Associations::ManyToOne.module_eval {
        extend DataMapper::CounterCacheable::ClassMethods
        
        (class << self; self; end).class_eval do
          unless method_defined?(:setup_without_counter_caching)
            alias_method :setup_without_counter_caching, :setup 
            alias_method :setup, :setup_with_counter_caching
          end
        end

      }
    end

    module ClassMethods
      
      def setup_with_counter_caching(name, model, options = {})
        counter_cache_attribute = options.delete(:counter_cache)
        relationship = setup_without_counter_caching(name, model, options)
        
        if counter_cache_attribute
          case counter_cache_attribute
            when String, Symbol
              counter_cache_attribute = counter_cache_attribute.to_s
            else
              counter_cache_attribute = "#{model.storage_name}_count".to_s
          end

          unless relationship.parent_model.has_property?(counter_cache_attribute.intern)
            relationship.parent_model.class_eval <<-EOS, __FILE__, __LINE__
              property :#{counter_cache_attribute}, Integer, :default => 0, :lazy => false          
            EOS            
          end

          model.class_eval <<-EOS, __FILE__, __LINE__            
            after :create, :increment_counter_cache_for_#{name}
            after :destroy, :decrement_counter_cache_for_#{name}
          
            def increment_counter_cache_for_#{name}
              if self.class == #{model.name}
                self.#{name}.update_attributes(:#{counter_cache_attribute} => self.#{name}.reload.#{counter_cache_attribute}.succ)
              end
            end

            def decrement_counter_cache_for_#{name}
              if self.class == #{model.name}
                self.#{name}.update_attributes(:#{counter_cache_attribute} => self.#{name}.reload.#{counter_cache_attribute} - 1)
              end
            end
            
          EOS
        end

        relationship
      end

    end # ClassMethods
{'fraction_non_alphanumeric': 0.07590016366612111, 'fraction_numerical': 0.0008183306055646482, 'mean_word_length': 2.274614869390489, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '3271772', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1269, 'n_words': 290}