<commit_before>require 'referehencible/version'
require 'securerandom'

module Referehencible
  def self.included(base)
    base.class_eval do
      ###
      # Validations
      #
      validates       :guid,
                        presence:         true,
                        uniqueness:       true,
                        format:           {
                          with:             /[a-f0-9]{32}/ },
                        length:           {
                          is:               32 }

      ###
      # Hooks
      #
      before_create   :generate_guid

      ###
      # ActiveRecord Overrides
      #
      def guid; generate_guid; end
    end
  end

private
  def generate_guid
    read_attribute(:guid) || write_attribute('guid', SecureRandom.hex(16))
  end
end<commit_msg>Convert referehencible over so that it has the ability to generate references for any number of attributes and add scopes for each one<commit_after>require 'referehencible/version'
require 'securerandom'

module Referehencible
  module ClassMethods
    def referenced_by(*referenced_attributes)
      referenced_attributes.each do |reference_attribute|
        validates       reference_attribute,
                          presence:         true,
                          uniqueness:       true,
                          format:           {
                            with:             /[a-f0-9]{32}/ },
                          length:           {
                            is:               32 }

        define_method(reference_attribute) do
          generate_guid(reference_attribute)
        end

        define_singleton_method("by_#{reference_attribute}") do |guid_to_find|
          where(:"#{reference_attribute}" => guid_to_find).
          first ||
          unknown_reference_object
        end

        after_initialize lambda { generate_guid reference_attribute }
      end

      private

      define_method(:generate_guid) do |reference_attribute|
        read_attribute(reference_attribute) || write_attribute(reference_attribute, SecureRandom.hex(16))
      end

      define_singleton_method(:unknown_reference_object) do
        return new unless respond_to?(:as_null_object)

        as_null_object
      end
    end
  end

  def self.included(base)
    base.extend ClassMethods
  end
end
{'fraction_non_alphanumeric': 0.06732844195079844, 'fraction_numerical': 0.0069054812257229176, 'mean_word_length': 1.6015712682379348, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1640576', 'n_tokens_mistral': 608, 'n_tokens_neox': 578, 'n_words': 129}