class Fias::Import::RestoreParentId

Public Class Methods

new(scope, options = {}) click to toggle source
# File lib/fias/import/restore_parent_id.rb, line 4
def initialize(scope, options = {})
  @scope = scope
  @key = options.fetch(:key, :aoguid)
  @parent_key = options.fetch(:parent_key, :parentguid)
  @id = options.fetch(:id, :id)
  @parent_id = options.fetch(:parent_id, :parent_id)
end

Public Instance Methods

restore() click to toggle source
# File lib/fias/import/restore_parent_id.rb, line 12
def restore
  id_grouped_by_parent_id.each do |parent_id, ids|
    @scope.where(id: ids).update(parent_id: parent_id)
  end
end

Private Instance Methods

id_grouped_by_parent_id() click to toggle source
# File lib/fias/import/restore_parent_id.rb, line 41
def id_grouped_by_parent_id
  {}.tap do |rows|
    id_parent_id_tuples.each do |(id, parent_id)|
      rows[parent_id] ||= []
      rows[parent_id] << id
    end
  end
end
id_parent_id_tuples() click to toggle source
# File lib/fias/import/restore_parent_id.rb, line 28
def id_parent_id_tuples
  records.map do |row|
    id, _, key = row

    if key
      parent_id = records_by_key[key]
      parent_id = parent_id[0] if parent_id
    end

    [id, parent_id]
  end
end
records() click to toggle source
# File lib/fias/import/restore_parent_id.rb, line 20
def records
  @records ||= @scope.select_map([@id, @key, @parent_key])
end
records_by_key() click to toggle source
# File lib/fias/import/restore_parent_id.rb, line 24
def records_by_key
  @records_by_key ||= records.index_by { |r| r[1] }
end