<commit_before>require 'active_record'
require 'active_record-readonly_model'
require 'active_record-enumerated_model/version'

module ActiveRecord
  module EnumeratedModel
    class UnknownAttributeError < StandardError; end
    class NilConstantError < StandardError; end

    module ClassMethods
      def create_enumeration_constants(constant_attribute = :name)
        self.all.each do |instance|
          constant_attribute = constant_attribute.to_s
          unless instance.attributes.include?(constant_attribute)
            raise UnknownAttributeError.new("Tried to create constants on the #{constant_attribute} attribute, but it doesn't exist on this model")
          end
          attribute_value = instance.attributes[constant_attribute]
          if attribute_value.blank?
            raise NilConstantError.new("Encountered a nil value on the #{constant_attribute} attribute and can't create a constant")
          end
          const = attribute_value.underscore.upcase
          const.gsub!(/\s/, '_')
          const.gsub!(/^[^A-Z]{1}/, '')
          const.gsub!(/[^A-Z_]/, '')
          const.gsub!(/[^A-Z]$/, '')
          unless self.const_defined?(const)
            self.const_set(const, instance)
          else
            raise RuntimeError.new("Constant #{self.to_s}::#{const} has already been defined")
          end
        end
      end
    end

    def self.included(caller)
      caller.extend(ClassMethods)
    end
  end
end<commit_msg>Including ReadonlyModel dependency<commit_after>require 'active_record'
require 'active_record-readonly_model'
require 'active_record-enumerated_model/version'

module ActiveRecord
  module EnumeratedModel
    class UnknownAttributeError < StandardError; end
    class NilConstantError < StandardError; end

    module ClassMethods
      def create_enumeration_constants(constant_attribute = :name)
        self.all.each do |instance|
          constant_attribute = constant_attribute.to_s
          unless instance.attributes.include?(constant_attribute)
            raise UnknownAttributeError.new("Tried to create constants on the #{constant_attribute} attribute, but it doesn't exist on this model")
          end
          attribute_value = instance.attributes[constant_attribute]
          if attribute_value.blank?
            raise NilConstantError.new("Encountered a nil value on the #{constant_attribute} attribute and can't create a constant")
          end
          const = attribute_value.underscore.upcase
          const.gsub!(/\s/, '_')
          const.gsub!(/^[^A-Z]{1}/, '')
          const.gsub!(/[^A-Z_]/, '')
          const.gsub!(/[^A-Z]$/, '')
          unless self.const_defined?(const)
            self.const_set(const, instance)
          else
            raise RuntimeError.new("Constant #{self.to_s}::#{const} has already been defined")
          end
        end
      end

      # TODO: add a bypass method that:
      #  - calls ActiveRecord::ReadonlyModel.bypass
      #  - reloads the enumeration
    end

    def self.included(caller)
      caller.extend(ClassMethods)
      caller.send(:include, ActiveRecord::ReadonlyModel)
    end
  end
end
{'fraction_non_alphanumeric': 0.09434561626429479, 'fraction_numerical': 0.0006353240152477764, 'mean_word_length': 2.7667464114832536, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3936994', 'n_tokens_mistral': 866, 'n_tokens_neox': 842, 'n_words': 202}