<commit_before>require 'delegate'

module SampleModels
  class Model < Delegator
    attr_reader :validation_collections
    
    def initialize(model_class)
      @model_class = model_class
      @validation_collections = Hash.new { |h, field|
        h[field] = ValidationCollection.new(self, field)
      }
    end
    
    def __getobj__
      @model_class
    end
    
    def associations(klass)
      @model_class.reflect_on_all_associations.select { |a|
        begin
          a.klass == klass
        rescue NameError
          false
        end
      }
    end
    
    def belongs_to_associations
      @model_class.reflect_on_all_associations.select { |assoc|
        assoc.macro == :belongs_to
      }
    end
    
    def construct_finder_sql(*args)
      if @model_class.method(:scoped).arity == -1
        @model_class.scoped.apply_finder_options(*args).arel.to_sql
      else
        @model_class.send(:construct_finder_sql, *args)
      end
    end
    
    def has_many_associations
      @model_class.reflect_on_all_associations.select { |assoc|
        assoc.macro == :has_many
      }
    end
    
    def record_validation(*args)
      type = args.shift
      config = args.extract_options!
      fields = args
      fields.each do |field|
        @validation_collections[field].add(type, config)
      end
    end
    
    def validates_presence_of?(attr)
      @validation_collections[attr].includes_presence?
    end
    
    def unique?(field, value)
      !@model_class.first(:conditions => {field => value})
    end
    
    class ValidationCollection
      def initialize(model, field)
        @model, @field = model, field
        @value_streams = []
      end
      
      def add(validation_method, config)
        stream_class_name = validation_method.to_s.camelize + 'ValueStream'
        if ValidationCollection.const_defined?(stream_class_name)
          stream_class = ValidationCollection.const_get(stream_class_name)
          input = @value_streams.last
          @value_streams << stream_class.new(@model, @field, config, input)
        end
      end
      
      def includes_presence?
        @value_streams.any? { |vs| vs.is_a?(ValidatesPresenceOfValueStream) }
      end
      
      def includes_uniqueness?
        @value_streams.any? { |vs| vs.is_a?(ValidatesUniquenessOfValueStream) }
      end
      
      def satisfying_value
        @value_streams.last.satisfying_value if @value_streams.last
      end
      
      class ValueStream
        attr_reader :input
        
        def initialize(model, field, config, input)
          @model, @field, @config, @input = model, field, config, input
          @sequence_number = 0
        end
      
        def column
          @model.columns.detect { |c| c.name == @field.to_s }
        end
        
        def increment
          @sequence_number += 1
          input.increment if input
        end
      end
      <commit_msg>uniqueness value stream is always at the end since it might need to force earlier streams to increment<commit_after>require 'delegate'

module SampleModels
  class Model < Delegator
    attr_reader :validation_collections
    
    def initialize(model_class)
      @model_class = model_class
      @validation_collections = Hash.new { |h, field|
        h[field] = ValidationCollection.new(self, field)
      }
    end
    
    def __getobj__
      @model_class
    end
    
    def associations(klass)
      @model_class.reflect_on_all_associations.select { |a|
        begin
          a.klass == klass
        rescue NameError
          false
        end
      }
    end
    
    def belongs_to_associations
      @model_class.reflect_on_all_associations.select { |assoc|
        assoc.macro == :belongs_to
      }
    end
    
    def construct_finder_sql(*args)
      if @model_class.method(:scoped).arity == -1
        @model_class.scoped.apply_finder_options(*args).arel.to_sql
      else
        @model_class.send(:construct_finder_sql, *args)
      end
    end
    
    def has_many_associations
      @model_class.reflect_on_all_associations.select { |assoc|
        assoc.macro == :has_many
      }
    end
    
    def record_validation(*args)
      type = args.shift
      config = args.extract_options!
      fields = args
      fields.each do |field|
        @validation_collections[field].add(type, config)
      end
    end
    
    def validates_presence_of?(attr)
      @validation_collections[attr].includes_presence?
    end
    
    def unique?(field, value)
      !@model_class.first(:conditions => {field => value})
    end
    
    class ValidationCollection
      def initialize(model, field)
        @model, @field = model, field
        @value_streams = []
      end
      
      def add(validation_method, config)
        stream_class_name = validation_method.to_s.camelize + 'ValueStream'
        if ValidationCollection.const_defined?(stream_class_name)
          stream_class = ValidationCollection.const_get(stream_class_name)
          if stream_class == ValidatesUniquenessOfValueStream
            @validates_uniqueness_config = config
          else
            input = @value_streams.last
            @value_streams << stream_class.new(@model, @field, config, input)
          end
        end
      end
      
      def includes_presence?
        @value_streams.any? { |vs| vs.is_a?(ValidatesPresenceOfValueStream) }
      end
      
      def includes_uniqueness?
        @value_streams.any? { |vs| vs.is_a?(ValidatesUniquenessOfValueStream) }
      end
      
      def satisfying_value
        if @validates_uniqueness_config
          input = @value_streams.last
          @value_streams << ValidatesUniquenessOfValueStream.new(
            @model, @field, @validates_uniqueness_config, input
          )
          @validates_uniqueness_config = nil
        end
        @value_streams.last.satisfying_value if @value_streams.last
      end
      
      class ValueStream
        attr_reader :input
        
        def initialize(model, field, config, input)
          @model, @field, @config, @input = model, field, config, input
          @sequence_number = 0
        end
      
        def column
          @model.columns.detect { |c| c.name == @field.to_s }
        end
        
        def increment
          @sequence_number += 1
          input.increment if input
        end
      end
      
{'fraction_non_alphanumeric': 0.07816669282687176, 'fraction_numerical': 0.0009417673834562863, 'mean_word_length': 2.1327433628318584, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '633493', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1897, 'n_words': 381}