Class: Filecamo::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/filecamo/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: Logger.new($stdout), txt_workers: {count: 4, queue_size: 5000}, bin_workers: {count: 4, queue_size: 5000}) ⇒ Generator

Returns a new instance of Generator.



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
# File 'lib/filecamo/generator.rb', line 10

def initialize(logger: Logger.new($stdout),
               txt_workers: {count: 4, queue_size: 5000},
               bin_workers: {count: 4, queue_size: 5000})
  @gen = Random.new
  @logger = logger
  @words_generated = {}
  @stats = {txt: 0, bin: 0}

  @txt_work_q = SizedQueue.new(txt_workers[:queue_size])
  @txt_workers = start_workers(:txt, @txt_work_q, txt_workers[:count]) do |file, len|
    while len > 0
      line = (LiterateRandomizer.sentence + $/)
      line.slice!(len..-1)
      len -= file.write(line)
    end
    @stats[:txt] += 1
  end

  @bin_work_q = SizedQueue.new(bin_workers[:queue_size])
  @bin_workers = start_workers(:bin, @bin_work_q, bin_workers[:count]) do |file, len|
    while len > 0
      len -= file.write(@gen.bytes(len < 32768 ? len : 32768))
    end
    @stats[:bin] += 1
  end
end

Instance Attribute Details

#statsObject (readonly)

Returns the value of attribute stats.



37
38
39
# File 'lib/filecamo/generator.rb', line 37

def stats
  @stats
end

Instance Method Details

#generate(min, max, count, depth, percent_text: 0, destination_path: nil, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/filecamo/generator.rb', line 39

def generate(min, max, count, depth, percent_text: 0, destination_path: nil, &block)
  min = BetterBytes.dehumanize(min)
  max = BetterBytes.dehumanize(max)
  count = count.to_i
  depth = depth.to_i
  percent_text = percent_text.to_f / 100
  dst_pn = Pathname.new(destination_path || '.')
  feed_workers(min, max, count, depth, percent_text, dst_pn, &block)
end

#killObject



57
58
59
60
61
62
# File 'lib/filecamo/generator.rb', line 57

def kill
  (@txt_workers + @bin_workers).each{|th| th.kill}
  ThreadsWait.all_waits(@txt_workers + @bin_workers) do |th|
    th.join
  end
end

#wait(sleep_interval: 0.3) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/filecamo/generator.rb', line 49

def wait(sleep_interval: 0.3)
  until @txt_work_q.empty? && @bin_work_q.empty?
    block_given? and
      yield(@txt_work_q.size, @bin_work_q.size)
    sleep(sleep_interval)
  end
end