<commit_before>require "unique_generator/version"
require "active_support/concern"

module UniqueGenerator
  extend ActiveSupport::Concern

  def self.setup!
    ActiveRecord::Base.send(:include, self) if defined?(ActiveRecord::Base)
    MongoMapper::Document.plugin(self)      if defined?(MongoMapper::Document)
  end

  CHOICES = (Array('A'..'Z') + Array('a'..'z') + Array('0'..'9'))

  included do
    if defined?(ActiveRecord::Base) && self <= ActiveRecord::Base
      include UniqueChecks::AR
    elsif defined?(MongoMapper::Document) && self <= MongoMapper::Plugins::Document
      include UniqueChecks::MM
    end
  end

  module UniqueChecks

    module MM
      # Returns whether or not the given field is unique to every item.
      def unique_field?(field_name)
        scope = self.class.where(field_name => send(field_name))
        scope = scope.where(:_id.ne => id) if id.present?
        scope.empty?
      end
    end

    module AR
      # Returns whether or not the given field is unique to every item.
      def unique_field?(field_name)
        scope = self.class.where(field_name => send(field_name))
        # When we have an id, check for those with a different id.
        if id.present?
          conditions = self.class.all.table[:id].eq(id).not
          scope = scope.where(conditions)
        end
        scope.empty?
      end
    end

  end

  module ClassMethods

    # Generate a unique field
    def generate_unique(length = 32, &blk)
      unique = generate_random(length)
      unique = generate_random(length) until blk.call(unique)
      unique
    end

    # Generate a random string with the given number of characters.
    def generate_random(length = 32)
      (1..length).inject("") { |token, _| token << CHOICES.sample }
    end

  end


  # Generate unique field.
  def generate_unique_field!(name, size = 32)
    return if send(name).present?
    self.class.generate_unique(size) do |token|
      send "#{name}=", token
      unique_field? name
    end
  end


  setup!

  if defined?(Rails::Railtie)
    class Railtie < Rails::Railtie
      initializer "unique_generator.initialize" do
        UniqueGenerator.setup!
      end
    end
  end

end<commit_msg>used arel_table for backwards compatablity with rails 3<commit_after>require "unique_generator/version"
require "active_support/concern"

module UniqueGenerator
  extend ActiveSupport::Concern

  def self.setup!
    ActiveRecord::Base.send(:include, self) if defined?(ActiveRecord::Base)
    MongoMapper::Document.plugin(self)      if defined?(MongoMapper::Document)
  end

  CHOICES = (Array('A'..'Z') + Array('a'..'z') + Array('0'..'9'))

  included do
    if defined?(ActiveRecord::Base) && self <= ActiveRecord::Base
      include UniqueChecks::AR
    elsif defined?(MongoMapper::Document) && self <= MongoMapper::Plugins::Document
      include UniqueChecks::MM
    end
  end

  module UniqueChecks

    module MM
      # Returns whether or not the given field is unique to every item.
      def unique_field?(field_name)
        scope = self.class.where(field_name => send(field_name))
        scope = scope.where(:_id.ne => id) if id.present?
        scope.empty?
      end
    end

    module AR
      # Returns whether or not the given field is unique to every item.
      def unique_field?(field_name)
        scope = self.class.where(field_name => send(field_name))
        # When we have an id, check for those with a different id.
        if id.present?
          conditions = self.class.arel_table[:id].eq(id).not
          scope = scope.where(conditions)
        end
        scope.empty?
      end
    end

  end

  module ClassMethods

    # Generate a unique field
    def generate_unique(length = 32, &blk)
      unique = generate_random(length)
      unique = generate_random(length) until blk.call(unique)
      unique
    end

    # Generate a random string with the given number of characters.
    def generate_random(length = 32)
      (1..length).inject("") { |token, _| token << CHOICES.sample }
    end

  end


  # Generate unique field.
  def generate_unique_field!(name, size = 32)
    return if send(name).present?
    self.class.generate_unique(size) do |token|
      send "#{name}=", token
      unique_field? name
    end
  end


  setup!

  if defined?(Rails::Railtie)
    class Railtie < Rails::Railtie
      initializer "unique_generator.initialize" do
        UniqueGenerator.setup!
      end
    end
  end

end
{'fraction_non_alphanumeric': 0.10307657758814283, 'fraction_numerical': 0.004266786436110487, 'mean_word_length': 2.8496110630942093, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '1037657', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1308, 'n_words': 366}