<commit_before>require 'taric_importer/helpers/string_helper'

class TaricImporter < TariffImporter
  class RecordProcessor
    include TaricImporter::Helpers::StringHelper

    # Meta record, containing all information from TARIC record as Hash
    attr_accessor :record

    # Operation: :create, :update, :destroy
    attr_accessor :operation

    # Entity class, e.g. Measure
    attr_accessor :klass

    # Entity primary key, i.e. Measure.primary_key
    attr_accessor :primary_key

    # TARIC transaction ID
    attr_accessor :transaction_id

    # The date of the update. NB not necessarily the current date.
    attr_accessor :operation_date

    # Sanitized and processed attributes
    attr_reader :attributes

    def initialize(record, operation_date = nil)
      self.operation_date = operation_date
      self.record = record
    end

    def record=(record)
      @record = record

      self.operation = record['update.type']
      self.transaction_id = record['transaction.id']
      self.klass = as_strategy(record.keys.last).constantize
      self.primary_key = [klass.primary_key].flatten.map(&:to_s)
      self.attributes = sanitize(record.values.last)
    end

    def attributes=(attributes)
      attributes = if attribute_processor = attributes_for(klass)
                     attribute_processor.call(attributes)
                   else
                     attributes
                   end

      @attributes = default_attributes.merge(attributes)
    end

    def default_attributes
      klass.columns.inject({}) { |memo, column_name|
        memo.merge!(Hash[column_name.to_s, nil])
        memo
      }
    end

    def operation=(operation)
      @operation = case operation
                   when "1" then :update
                   when "2" then :destroy
                   when "3" then :create
                   else
                     ActiveSupport::Notifications.instrument("taric_unexpected_update_type.tariff_importer", record: record)

                     raise TaricImporter::ImportException.new
                   end
    end

    def process!
      if processor_override = processor_for(klass, operation)
        processor_override.call(self)
      else
        default_process
      end
    end

    private

    def default_process
      case operation
      when :create
        model = klass.new(attributes)
        model.save(validate: false) # defer validation, as associated
                                    # records in the same transaction
                                    # may not been saved yet
        model
      when :destroy
        model = klass.filter(attributes.slice(*primary_key).symbolize_keys).first
        model.set(attributes.except(*primary_key).symbolize_keys)
        model.destroy
        nil
      when :update
        model = klass.filter(attributes.slice(*primary_key).symbolize_keys).first
        model.set(attributes.except(*primary_key).symbolize_keys)
        model.save(validate: false)
        model
      end<commit_msg>Remove outdated comment<commit_after>require 'taric_importer/helpers/string_helper'

class TaricImporter < TariffImporter
  class RecordProcessor
    include TaricImporter::Helpers::StringHelper

    # Meta record, containing all information from TARIC record as Hash
    attr_accessor :record

    # Operation: :create, :update, :destroy
    attr_accessor :operation

    # Entity class, e.g. Measure
    attr_accessor :klass

    # Entity primary key, i.e. Measure.primary_key
    attr_accessor :primary_key

    # TARIC transaction ID
    attr_accessor :transaction_id

    # The date of the update. NB not necessarily the current date.
    attr_accessor :operation_date

    # Sanitized and processed attributes
    attr_reader :attributes

    def initialize(record, operation_date = nil)
      self.operation_date = operation_date
      self.record = record
    end

    def record=(record)
      @record = record

      self.operation = record['update.type']
      self.transaction_id = record['transaction.id']
      self.klass = as_strategy(record.keys.last).constantize
      self.primary_key = [klass.primary_key].flatten.map(&:to_s)
      self.attributes = sanitize(record.values.last)
    end

    def attributes=(attributes)
      attributes = if attribute_processor = attributes_for(klass)
                     attribute_processor.call(attributes)
                   else
                     attributes
                   end

      @attributes = default_attributes.merge(attributes)
    end

    def default_attributes
      klass.columns.inject({}) { |memo, column_name|
        memo.merge!(Hash[column_name.to_s, nil])
        memo
      }
    end

    def operation=(operation)
      @operation = case operation
                   when "1" then :update
                   when "2" then :destroy
                   when "3" then :create
                   else
                     ActiveSupport::Notifications.instrument("taric_unexpected_update_type.tariff_importer", record: record)

                     raise TaricImporter::ImportException.new
                   end
    end

    def process!
      if processor_override = processor_for(klass, operation)
        processor_override.call(self)
      else
        default_process
      end
    end

    private

    def default_process
      case operation
      when :create
        model = klass.new(attributes)
        model.save(validate: false)
        model
      when :destroy
        model = klass.filter(attributes.slice(*primary_key).symbolize_keys).first
        model.set(attributes.except(*primary_key).symbolize_keys)
        model.destroy
        nil
      when :update
        model = klass.filter(attributes.slice(*primary_key).symbolize_keys).first
        model.set(attributes.except(*primary_key).symbolize_keys)
        model.save(validate: false)
        model
      end
{'fraction_non_alphanumeric': 0.07343192248852626, 'fraction_numerical': 0.0010198878123406426, 'mean_word_length': 2.2688888888888887, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7574146', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1528, 'n_words': 363}