Class: PopulateCedictTable

Inherits:
ActiveRecord::Migration
  • Object
show all
Defined in:
lib/generators/analects/cedict/templates/populate_cedict_table.rb

Instance Method Summary collapse

Instance Method Details

#downObject



38
39
40
# File 'lib/generators/analects/cedict/templates/populate_cedict_table.rb', line 38

def down
  Cedict.delete_all
end

#upObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/generators/analects/cedict/templates/populate_cedict_table.rb', line 5

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