Class: BigMailGenerator

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

Instance Method Summary collapse

Constructor Details

#initialize(input, output, number) ⇒ BigMailGenerator

Returns a new instance of BigMailGenerator.



5
6
7
8
9
# File 'lib/big_mail_generator.rb', line 5

def initialize(input, output, number)
    @input = input
    @output = output
    @number = number
end

Instance Method Details

#cpallObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/big_mail_generator.rb', line 23

def cpall
    if File.directory?(@input)
        inputfiles = Dir.glob(File.join(@input, '*'))
    else
        inputfiles = [@input]
    end

    count = 0
    copycounts = @number / inputfiles.length
    @digit = copycounts.to_s.length + 1
    inputfiles.each do |ifile|
        size = File.size(ifile)
        puts "copy #{ifile}(#{size / 1024}KB) #{copycounts} times"
        copycounts.times do |i| 
            cpfile ifile, i
            count += 1
            puts "#{count} files copied." if (count % 3000 == 0)
        end
    end

    remains = @number % inputfiles.length
    remains.times do |i| 
        cpfile inputfiles[i], copycounts + i
        count += 1
        puts "#{count} files copied." if (count % 3000 == 0)
    end

    puts "Done. #{count} files copied."
end

#cpfile(ifile, i) ⇒ Object



53
54
55
56
57
58
# File 'lib/big_mail_generator.rb', line 53

def cpfile(ifile, i)
    fn = File.basename(ifile)
    pos = fn.rindex('.')
    ofile = File.join(@output, "#{fn[0..(pos-1)]}#{(i+1).to_s.rjust(@digit, '0')}#{fn[pos..-1]}")
    FileUtils.cp(ifile, ofile)
end

#generateObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/big_mail_generator.rb', line 11

def generate
    start = Time.now
    cpall
    diff = Time.now - start
    if diff / 60 < 1
        puts "Elapsed time: #{diff.round(3)} seconds." 
    else
        puts "Elapsed time: #{(diff / 60).round(1)} minutes." 
    end
    system("du -h #{@output}")
end