Sha256: c679e951d97cbbced8a605e7d6c7307694a901dc807f4744f048fb653efb9c47

Contents?: true

Size: 1.09 KB

Versions: 6

Compression:

Stored size: 1.09 KB

Contents

require 'analects/cedict'
require 'analects/cli/progress'

class PopulateCedictTable < ActiveRecord::Migration
  def up
    path = ENV['CEDICT_PATH'] || Analects::CedictLoader::LOCAL
    unless File.exist? path
      puts "-- cedict file not found, downloading"
      Analects::CedictLoader.download!
    end

    if File.exist? path
      f = File.open path
      l = Analects::CedictLoader.new(f)
      puts "-- Inserting CC-CEDICT"
      l.headers.each do |k,v|
        puts "     #{k}=#{v}"
      end
      p = Analects::CLI::Progress.new(Integer(l.headers['entries'])-1, 5000, '   ')
      Cedict.transaction do
        l.each do |traditional, simplified, pinyin, english|
          p.next
          Cedict.create!(
                         :traditional => traditional,
                         :simplified => simplified,
                         :pinyin => pinyin,
                         :english => english
                         )
        end
      end
      f.close
      puts
    else
      raise "CC-Cedict file not found and failed to download"
    end
  end

  def down
    Cedict.delete_all
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
analects-0.4.2 lib/generators/analects/cedict/templates/populate_cedict_table.rb
analects-0.4.1 lib/generators/analects/cedict/templates/populate_cedict_table.rb
analects-0.4.0 lib/generators/analects/cedict/templates/populate_cedict_table.rb
analects-0.3.1 lib/generators/analects/cedict/templates/populate_cedict_table.rb
analects-0.2.1 lib/generators/analects/cedict/templates/populate_cedict_table.rb
analects-0.2.0 lib/generators/analects/cedict/templates/populate_cedict_table.rb