<commit_before>module Dataly
  class Mapper
    attr_reader :model

    def self.field(from, symbol_or_proc)
      fields[from] = symbol_or_proc
    end

    def self.fields
      @@fields ||= {}
    end

    def initialize(model)
      @model = model
    end

    def process(row)
      row.map { |name, value|
        name = switch(name, value)
        value = transform(name, value)
        [name.to_sym, (value.blank? ? nil : value)] if attributes.include?(name.to_s)
      }.compact.to_h
    end

    def switch(name, value)
      @@fields[name] || name
    end

    def transform(name, value)
      if @@fields[name].respond_to?(:call)
        @@fields[name].call(value)
      else
        value
      end
    end

    def attributes
      @model.attribute_names
    end

  end
end<commit_msg>Use ActiveSupport for class Attributes<commit_after>module Dataly
  class Mapper
    attr_reader :model
    cattr_reader :fields

    def self.field(from, symbol_or_proc)
      fields[from] = symbol_or_proc
    end

    def initialize(model)
      @model = model
    end

    def process(row)
      row.map { |name, value|
        name = switch(name, value)
        value = transform(name, value)
        [name.to_sym, (value.blank? ? nil : value)] if attributes.include?(name.to_s)
      }.compact.to_h
    end

    def switch(name, value)
      fields[name] || name
    end

    def transform(name, value)
      if fields[name].respond_to?(:call)
        fields[name].call(value)
      else
        value
      end
    end

    def attributes
      @model.attribute_names
    end

  end
end
{'fraction_non_alphanumeric': 0.09993714644877436, 'fraction_numerical': 0.0, 'mean_word_length': 2.190380761523046, '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': '73171', 'n_tokens_mistral': 525, 'n_tokens_neox': 503, 'n_words': 125}