extract_sies_data.rb 1.17 KB
$LOAD_PATH.unshift('/usr/share/rails/activerecord/lib')
$LOAD_PATH.unshift('/usr/share/rails/activesupport/lib')

require 'activerecord'
require 'active_support'

LIMIT = 5

ActiveRecord::Base.establish_connection(
  :adapter => 'mysql',
  :host => 'localhost',
  :database => 'farejador',
  :username => 'root',
  :password => 'root'
)

class Enterprise < ActiveRecord::Base
  set_table_name 'cons_dadosbasicos'
end

class Category < ActiveRecord::Base
  set_table_name 'lista_produtos'
end

class Dumper
  def initialize
    @seq = 0
    @seqs = {}
  end

  def pretty(str, alt = nil)
    if alt.nil?
      str
    else
      str + ' (' + alt + ')'
    end
  end

  def dump(cat, parent = nil)

    @seqs[cat] = @seq
    puts "cat#{@seq} = Category.create!(:name => #{pretty(cat.nome, cat.nome_alt).inspect}, :parent => #{parent ? 'cat' + @seqs[parent].to_s : 'nil' })"
    @seq = @seq + 1

    Category.find(:all, :conditions => { :id_mae => cat.id }).each do |child|
      dump(child, cat)
    end

  end

end

dumper = Dumper.new
Category.find(:all, :conditions => 'id_mae is null or id_mae = -1').each do |cat|
  dumper.dump(cat, nil)
end

# puts Enterprise.find(:all, :limit => LIMIT).to_xml