<commit_before>module Document
  module Base
    extend ActiveSupport::Concern

    class_methods do
      def has_document(options)
        class_name = options[:class_name]
        @@document_class = self.const_get(class_name)
      end

      def document_class
        @@document_class
      end
    end

    included do
      class_eval do
        validate :document_valid
      end

      def document
        @document ||= self.class.document_class.new(read_attribute(:document))
      end

      def document=(attributes)
        @document = nil
        filtered_attributes = filter_date_params(attributes)
        write_attribute(:document, self.class.document_class.new(filtered_attributes).to_json)
      end

      protected

      def filter_date_params(params)
        params = params.dup # DISCUSS: not sure if that slows down form processing?
        date_attributes = {}

        params.each do |attribute, value|
          if value.is_a?(Hash)
            params[attribute] = filter_date_params(value)
          elsif matches = attribute.match(/^(\w+)\(.i\)$/)
            date_attribute = matches[1]
            date_attributes[date_attribute] = params_to_date(
              params.delete("#{date_attribute}(1i)"),
              params.delete("#{date_attribute}(2i)"),
              params.delete("#{date_attribute}(3i)"),
              params.delete("#{date_attribute}(4i)"),
              params.delete("#{date_attribute}(5i)")
            )
          end
        end
        params.merge!(date_attributes)
      end

      def params_to_date(year, month, day, hour, minute)
        date_fields = [year, month, day].map!(&:to_i)
        time_fields = [hour, minute].map!(&:to_i)
<commit_msg>Fix document_class bug when 2 classes using the Base class<commit_after>module Document
  module Base
    extend ActiveSupport::Concern

    class_methods do
      def has_document(options)
        class_name = options[:class_name]
        document_class = self.const_get(class_name)

        define_method("document_class".to_sym) do
          document_class
        end
      end
    end

    included do
      class_eval do
        validate :document_valid
      end

      def document
        @document ||= document_class.new(read_attribute(:document))
      end

      def document=(attributes)
        @document = nil
        filtered_attributes = filter_date_params(attributes)
        write_attribute(:document, document_class.new(filtered_attributes).to_json)
      end

      protected

      def filter_date_params(params)
        params = params.dup # DISCUSS: not sure if that slows down form processing?
        date_attributes = {}

        params.each do |attribute, value|
          if value.is_a?(Hash)
            params[attribute] = filter_date_params(value)
          elsif matches = attribute.match(/^(\w+)\(.i\)$/)
            date_attribute = matches[1]
            date_attributes[date_attribute] = params_to_date(
              params.delete("#{date_attribute}(1i)"),
              params.delete("#{date_attribute}(2i)"),
              params.delete("#{date_attribute}(3i)"),
              params.delete("#{date_attribute}(4i)"),
              params.delete("#{date_attribute}(5i)")
            )
          end
        end
        params.merge!(date_attributes)
      end

      def params_to_date(year, month, day, hour, minute)
        date_fields = [year, month, day].map!(&:to_i)
        time_fields = [hour, minute].map!(&:to_i)

{'fraction_non_alphanumeric': 0.10449050086355786, 'fraction_numerical': 0.003742084052964882, 'mean_word_length': 2.3381364073006723, 'pattern_counts': {'":': 0, '<': 3, '<?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': '311760', 'n_tokens_mistral': 990, 'n_tokens_neox': 960, 'n_words': 195}