Sha256: a934eed7a91218cad417a1c5f5bd8b4f7b843234d2342893fdfeca37373b97b6

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require 'active_record'
require 'openssl'

module MasterDataTool
  class MasterDataStatus < ::ActiveRecord::Base
    self.table_name = 'master_data_statuses'

    validates :name,
              presence: true

    validates :version,
              presence: true

    def will_change?(master_data_file)
      raise unless name == master_data_file.table_name

      version != self.class.decide_version(master_data_file.path)
    end

    def model_klass
      Object.const_get(name.classify)
    end

    class << self
      def build(spec_name:, master_data_file:)
        version = decide_version(master_data_file.path)
        name = MasterDataTool.resolve_table_name(spec_name: spec_name, csv_path: master_data_file.path, override_identifier: master_data_file.override_identifier)
        new(spec_name: spec_name, name: name, version: version)
      end

      def import_records!(records:, dry_run: true)
        if dry_run
          pp records
          return
        end

        return if records.empty?

        import_records = records.map { |obj| obj.attributes.slice(*import_columns) }
        upsert_all(import_records, update_only: %w[version])
      end

      def decide_version(csv_path)
        OpenSSL::Digest::SHA256.hexdigest(File.open(csv_path).read)
      end

      private

      def import_columns
        %w[spec_name name version]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
master_data_tool-0.23.0 lib/master_data_tool/master_data_status.rb