Skip to content

Instantly share code, notes, and snippets.

@jagbolanos
Last active December 30, 2015 05:29
Show Gist options
  • Save jagbolanos/7782766 to your computer and use it in GitHub Desktop.
Save jagbolanos/7782766 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'csv'
# 3 dept, 4 mun, 1 mesa
departamento = nil
municipio = nil
primero = nil
ultimo = nil
departamentos = Hash.new
CSV.foreach("departamentos.csv") do |row|
iddepartamento = sprintf("%02d", row[0].to_i)
departamentos["#{iddepartamento}"] = {:nombre => row[1], :municipios => Hash.new }
end
CSV.foreach("municipios.csv") do |row|
iddepartamento = sprintf("%02d", row[1].to_i)
idmunicipio = sprintf("%02d", row[2].to_i)
departamentos["#{iddepartamento}"][:municipios]["#{idmunicipio}"] = row[3]
end
CSV.foreach("mesas.csv") do |row|
if departamento != row[3] then
if departamento != nil then
puts "Municipio.create(:name=>\"#{departamentos[departamento][:municipios][municipio]}\",:num=>#{municipio.to_i},:departamento_id=>departamento.id, :actum_from=>#{primero},:actum_to=>#{ultimo})"
end
departamento = row[3]
puts "departamento = Departamento.create(:name=>\"#{departamentos[row[3]][:nombre]}\",:num=>#{row[3].to_i})"
municipio = nil
end
if municipio != row[4] then
if municipio != nil then
puts "Municipio.create(:name=>\"#{departamentos[departamento][:municipios][municipio]}\",:num=>#{municipio.to_i},:departamento_id=>departamento.id, :actum_from=>#{primero},:actum_to=>#{ultimo})"
end
primero = row[1]
ultimo = row[1]
municipio = row[4]
else
ultimo = row[1]
end
end
puts "Municipio.create(:name=>\"#{departamentos[departamento][:municipios][municipio]}\",:num=>#{municipio.to_i},:departamento_id=>departamento.id, :actum_from=>#{primero},:actum_to=>#{ultimo})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment