Sha256: 8f7b91efc4ee3b4b061d034f1088363b56f9e65f52988eefa255774fccc087a9

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

module AbstractImporter
  class IdMap

    class IdNotMappedError < StandardError; end

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



    def init(table_name, query)
      table_name = table_name.to_sym
      @id_map[table_name] = {}
      merge! table_name, query
    end

    def merge!(table_name, query)
      table_name = table_name.to_sym
      @id_map[table_name].merge! Hash[query.pluck(:legacy_id, :id)]
    end

    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!(legacy_id, depends_on)
      return nil if legacy_id.blank?
      id_map = @id_map[depends_on]
      raise IdNotMappedError.new unless id_map.key?(legacy_id)
      id_map[legacy_id]
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
abstract_importer-1.3.4 lib/abstract_importer/id_map.rb
abstract_importer-1.3.3 lib/abstract_importer/id_map.rb
abstract_importer-1.3.2 lib/abstract_importer/id_map.rb
abstract_importer-1.3.1 lib/abstract_importer/id_map.rb