<commit_before>require 'sequel'

module Sequel
  class Annotate
    # Append/replace the schema comment for all of the given files.
    # Attempts to guess the model for each file using a regexp match of
    # the file's content, if this doesn't work, you'll need to create
    # an instance manually and pass in the model and path. Example:
    # 
    #   Sequel::Annotate.annotate(Dir['models/*.rb'])
    def self.annotate(paths)
      Sequel.extension :inflector
      paths.each do |path|
        if match = File.read(path).match(/class (\S+)\s*<\s*Sequel::Model/)
          new(match[1].constantize).annotate(path)
        end
      end
    end

    # The model to annotate
    attr_reader :model

    # Store the model to annotate
    def initialize(model)
      @model = model 
    end

    # Append the schema comment (or replace it if one already exists) to
    # the file at the given path.
    def annotate(path)
      orig = current = File.read(path).rstrip

      if m = current.reverse.match(/#{"#{$/}# Table: ".reverse}/m)
        offset = current.length - m.end(0) + 1
        unless current[offset..-1].match(/^[^#]/)
          # If Table: comment exists, and there are no
          # uncommented lines between it and the end of the file
          # then replace current comment instead of appending it
          current = current[0...offset].rstrip
        end
      end

      current += "#{$/}#{$/}#{schema_comment}"

      if orig != current
        File.open(path, "wb") do |f|
          f.puts current
        end
      end
    end

    # The schema comment to use for this model.  
    # For all databases, includes columns, indexes, and foreign
    # key constraints in this table referencing other tables.
    # On PostgreSQL, also includes check constraints, triggers,
    # and foreign key constraints in other tables referencing this table.
    def schema_comment
      output = []
      output << "# Table: #{model.table_name}"

      meth = :"_schema_comment_#{model.db.database_type}"
      if respond_to?(meth, true)
        send(meth, output)
      else
        schema_comment_columns(output)
        schema_comment_indexes(output)<commit_msg>Try to be more similar to active_record-annotate<commit_after>require 'sequel'

module Sequel
  class Annotate
    # Append/replace the schema comment for all of the given files.
    # Attempts to guess the model for each file using a regexp match of
    # the file's content, if this doesn't work, you'll need to create
    # an instance manually and pass in the model and path. Example:
    # 
    #   Sequel::Annotate.annotate(Dir['models/*.rb'])
    def self.annotate(paths)
      Sequel.extension :inflector
      paths.each do |path|
        if match = File.read(path).match(/class (\S+)\s*<\s*Sequel::Model/)
          new(match[1].constantize).annotate(path)
        end
      end
    end

    # The model to annotate
    attr_reader :model

    # Store the model to annotate
    def initialize(model)
      @model = model 
    end

    # Append the schema comment (or replace it if one already exists) to
    # the file at the given path.
    def annotate(path, position = :after)
      orig = current = File.read(path).rstrip

      if position == :after
        
        if m = current.reverse.match(/#{"#{$/}# Table: ".reverse}/m)
          offset = current.length - m.end(0) + 1
          unless current[offset..-1].match(/^[^#]/)
            # If Table: comment exists, and there are no
            # uncommented lines between it and the end of the file
            # then replace current comment instead of appending it
            current = current[0...offset].rstrip
          end
        end
        current += "#{$/}#{$/}#{schema_comment}"

      elsif position == :before

        current = current.gsub(/\A#\sTable[^\n\r]+\r?\n(?:#[^\n\r]*\r?\n)*/m, '').lstrip
        current = "#{schema_comment}#{$/}#{$/}#{current}"

      end

      if orig != current
        File.open(path, "wb") do |f|
          f.puts current
        end
      end
    end

    # The schema comment to use for this model.  
    # For all databases, includes columns, indexes, and foreign
    # key constraints in this table referencing other tables.
    # On PostgreSQL, also includes check constraints, triggers,
    # and foreign key constraints in other tables referencing this table.
    def schema_comment
      output = []
      output << "# Table: #{model.table_name}"

      meth = :"_schema_comment_#{model.db.database_type}"
      if respond_to?(meth, true)
        send(meth, output)
      else
        schema_comment_columns(output)
        schema_comment_indexes(output)
{'fraction_non_alphanumeric': 0.10738831615120274, 'fraction_numerical': 0.002147766323024055, 'mean_word_length': 2.355187319884726, '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': '1741815', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1334, 'n_words': 455}