<commit_before>require 'csv_row_model/model/dynamic_column_cell'

module CsvRowModel
  module Model
    module DynamicColumns
      extend ActiveSupport::Concern
      included do
        inherited_class_hash :dynamic_columns
      end

      # See Model::Columns#attributes
      def attributes
        super.merge!(attributes_from_method_names(self.class.dynamic_column_names))
      end

      class_methods do
        def dynamic_columns?
          dynamic_columns.present?
        end

        def is_dynamic_column?(column_name)
          dynamic_columns.keys.include?(column_name)
        end

        # Safe to override. Method applied to each dynamic_column attribute
        #
        # @param cells [Array] Array of values
        # @param column_name [Symbol] Dynamic column name
        def format_dynamic_column_cells(cells, column_name, column_index, context)
          cells
        end

        # Safe to override
        #
        # @return [String] formatted header
        def format_dynamic_column_header(header_model, column_name, dynamic_column_index, context)
          header_model
        end

        # See Model::Columns::headers
        def headers(context={})
          super + dynamic_column_headers(context)
        end

        def dynamic_column_headers(context={})
          dynamic_column_names.map { |column_name| DynamicColumnHeader.new(column_name, self, context).value }.flatten
        end

        # @return [Integer] index of dynamic_column of all columns
        def dynamic_column_index(column_name)
          offset = dynamic_column_names.index(column_name)
          offset ? columns.size + offset : nil
        end

        # @return [Array<Symbol>] column names for the row model
        def dynamic_column_names
          dynamic_columns.keys
        end

        protected

        VALID_OPTIONS_KEYS = %i[header].freeze

        # define a dynamic_column, must be after all normal columns<commit_msg>add header_models_context_key to valid option<commit_after>require 'csv_row_model/model/dynamic_column_cell'

module CsvRowModel
  module Model
    module DynamicColumns
      extend ActiveSupport::Concern
      included do
        inherited_class_hash :dynamic_columns
      end

      # See Model::Columns#attributes
      def attributes
        super.merge!(attributes_from_method_names(self.class.dynamic_column_names))
      end

      class_methods do
        def dynamic_columns?
          dynamic_columns.present?
        end

        def is_dynamic_column?(column_name)
          dynamic_columns.keys.include?(column_name)
        end

        # Safe to override. Method applied to each dynamic_column attribute
        #
        # @param cells [Array] Array of values
        # @param column_name [Symbol] Dynamic column name
        def format_dynamic_column_cells(cells, column_name, column_index, context)
          cells
        end

        # Safe to override
        #
        # @return [String] formatted header
        def format_dynamic_column_header(header_model, column_name, dynamic_column_index, context)
          header_model
        end

        # See Model::Columns::headers
        def headers(context={})
          super + dynamic_column_headers(context)
        end

        def dynamic_column_headers(context={})
          dynamic_column_names.map { |column_name| DynamicColumnHeader.new(column_name, self, context).value }.flatten
        end

        # @return [Integer] index of dynamic_column of all columns
        def dynamic_column_index(column_name)
          offset = dynamic_column_names.index(column_name)
          offset ? columns.size + offset : nil
        end

        # @return [Array<Symbol>] column names for the row model
        def dynamic_column_names
          dynamic_columns.keys
        end

        protected

        VALID_OPTIONS_KEYS = %i[header header_models_context_key].freeze

        # define a dynamic_column, must be after all normal columns
{'fraction_non_alphanumeric': 0.05956587582029278, 'fraction_numerical': 0.0, 'mean_word_length': 2.494708994708995, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3942593', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1015, 'n_words': 264}