Class: CipherBureau::DataLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/cipher_bureau/data_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language, chunks = 100, words = 0, verbose = true) ⇒ DataLoader

Returns a new instance of DataLoader.



30
31
32
# File 'lib/cipher_bureau/data_loader.rb', line 30

def initialize(language, chunks = 100, words = 0, verbose = true)
  @language, @chunks, @words, @verbose = language, chunks, words, verbose
end

Instance Attribute Details

#chunksObject

Returns the value of attribute chunks.



28
29
30
# File 'lib/cipher_bureau/data_loader.rb', line 28

def chunks
  @chunks
end

#languageObject (readonly)

Returns the value of attribute language.



27
28
29
# File 'lib/cipher_bureau/data_loader.rb', line 27

def language
  @language
end

#wordsObject (readonly)

Returns the value of attribute words.



27
28
29
# File 'lib/cipher_bureau/data_loader.rb', line 27

def words
  @words
end

Instance Method Details

#process(*directories_and_filename, &block) ⇒ Object



60
61
62
63
# File 'lib/cipher_bureau/data_loader.rb', line 60

def process(*directories_and_filename, &block)
  filename = ([CipherBureau.language_repository, language.to_s] + directories_and_filename).join('/')
  writer(reader(filename), &block)
end

#reader(filename) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/cipher_bureau/data_loader.rb', line 34

def reader(filename)
  Fiber.new do
    puts "Reading file #{filename}" if @verbose
    open(filename).readlines.each do |str|
      Fiber.yield str
    end
    puts "\r#{@words}" if @verbose
    nil
  end
end

#writer(fiber, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cipher_bureau/data_loader.rb', line 45

def writer(fiber, &block)
  str = nil
  begin
    chunks.times do
      ActiveRecord::Base.transaction do
        break unless str = fiber.resume
        yield str.chomp
        @words += 1
      end
      return unless str
    end
    print "\r#{@words}" if @verbose
  end while str
end