Sha256: 04422e0ada11d954078da979f78d65e971358af0c67747640c953b8ba10f6825

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

require 'active_record'
require 'activerecord-import'

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

    validates :name,
              presence: true

    validates :version,
              presence: true

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

      def import_records!(records, dry_run: true)
        if dry_run
          pp records
        else
          import!(records, validate: true, on_duplicate_key_update: %w[name version], timestamps: true)
        end
      end

      def master_data_will_change?(csv_path)
        new_version = decide_version(csv_path)
        !where(name: MasterDataTool.resolve_table_name(csv_path), version: new_version).exists?
      end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
master_data_tool-0.6.0 lib/master_data_tool/master_data_status.rb
master_data_tool-0.5.0 lib/master_data_tool/master_data_status.rb
master_data_tool-0.4.0 lib/master_data_tool/master_data_status.rb
master_data_tool-0.3.0 lib/master_data_tool/master_data_status.rb
master_data_tool-0.2.0 lib/master_data_tool/master_data_status.rb
master_data_tool-0.1.0 lib/master_data_tool/master_data_status.rb