Sha256: f3536ae6c7d0314b813f97d6743e5e95523fc0eb5654722bb32cedca640acb42

Contents?: true

Size: 1.38 KB

Versions: 13

Compression:

Stored size: 1.38 KB

Contents

module AbstractImporter
  class IdMap

    def initialize
      @id_map = Hash.new { |hash, key| hash[key] = {} }
    end

    def merge!(table_name, map)
      table_name = table_name.to_sym
      map = Hash[map.where.not(legacy_id: nil).pluck(:legacy_id, :id)] unless map.is_a?(Hash)
      @id_map[table_name].merge! map
    end
    alias :init :merge!

    def get(table_name)
      @id_map[table_name.to_sym].dup
    end
    alias :[] :get

    def contains?(table_name, id)
      @id_map[table_name.to_sym].key?(id)
    end

    def <<(record)
      register(record: record)
    end

    def register(options={})
      if options.key?(:record)
        record = options[:record]
        table_name, record_id, legacy_id = record.class.table_name, record.id, record.legacy_id
      end
      table_name = options[:table_name] if options.key?(:table_name)
      legacy_id = options[:legacy_id] if options.key?(:legacy_id)
      record_id = options[:record_id] if options.key?(:record_id)

      table_name = table_name.to_sym
      @id_map[table_name][legacy_id] = record_id
    end

    def apply!(depends_on, legacy_id)
      return nil if legacy_id.blank?
      @id_map[depends_on].fetch(legacy_id)
    end

    def tables
      @id_map.keys
    end

    def dup
      IdMap.new.tap do |other|
        tables.each do |table|
          other.init table, get(table)
        end
      end
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
abstract_importer-1.9.0 lib/abstract_importer/id_map.rb
abstract_importer-1.8.0 lib/abstract_importer/id_map.rb
abstract_importer-1.7.0 lib/abstract_importer/id_map.rb
abstract_importer-1.6.0 lib/abstract_importer/id_map.rb
abstract_importer-1.5.6 lib/abstract_importer/id_map.rb
abstract_importer-1.5.5 lib/abstract_importer/id_map.rb
abstract_importer-1.5.4 lib/abstract_importer/id_map.rb
abstract_importer-1.5.3 lib/abstract_importer/id_map.rb
abstract_importer-1.5.2 lib/abstract_importer/id_map.rb
abstract_importer-1.5.1 lib/abstract_importer/id_map.rb
abstract_importer-1.5.0 lib/abstract_importer/id_map.rb
abstract_importer-1.4.1 lib/abstract_importer/id_map.rb
abstract_importer-1.4.0 lib/abstract_importer/id_map.rb