Class: Batch

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

Constant Summary collapse

VERSION =
"1.0.4"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enumerable) ⇒ Batch

Returns a new instance of Batch.



6
7
8
9
10
11
# File 'lib/batch.rb', line 6

def initialize(enumerable)
  @enumerable = enumerable
  @width = (ENV["BATCH_WIDTH"] || 75).to_i
  @size = enumerable.size if enumerable.respond_to?(:size)
  @debug = $DEBUG || (ENV["BATCH_DEBUG"] || "0").to_i == 1
end

Instance Attribute Details

#enumerableObject (readonly)

Returns the value of attribute enumerable.



4
5
6
# File 'lib/batch.rb', line 4

def enumerable
  @enumerable
end

Class Method Details

.each(enumerable, &block) ⇒ Object



92
93
94
# File 'lib/batch.rb', line 92

def self.each(enumerable, &block)
  new(enumerable).each(&block)
end

.interactive?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/batch.rb', line 116

def self.interactive?
  (ENV["BATCH_INTERACTIVE"] || 1).to_i == 1
end

.out(&block) ⇒ Object



112
113
114
# File 'lib/batch.rb', line 112

def self.out(&block)
  interactive? ? yield($stdout) : File.open("/dev/null", "w", &block)
end

.start(title, enumerable, &block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/batch.rb', line 96

def self.start(title, enumerable, &block)
  begin
    enumerable.each.next
  rescue StopIteration
    return
  end

  out do |io|
    io.puts
    io.puts(title)
    io.puts
  end

  each(enumerable, &block)
end

Instance Method Details

#each(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
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
52
53
# File 'lib/batch.rb', line 13

def each(&block)
  @errors = []
  @current = 0

  Batch.out do |io|
    enumerable.each.with_index do |item, index|
      report_progress(io) if index == 0

      begin
        yield(item)
        io.print "."

      rescue Interrupt => e
        report_errors
        raise e

      rescue Exception => e
        raise e if @debug
        io.print "E"
        @errors << [item, e]

      ensure
        @current += 1

        if eol?
          io.print "\n"
          report_progress(io)
        end
      end
    end

    if @current > 0
      io.print "\n"
      io.puts "100% " unless eol?

      report_errors
    end
  end

  nil
end

#eol?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/batch.rb', line 88

def eol?
  @current % @width == 0
end

#progressObject



73
74
75
76
77
78
# File 'lib/batch.rb', line 73

def progress
  return unless @size
  return 0 if @size == 0

  @current * 100 / @size
end

#report_error(item, error) ⇒ Object



65
66
67
# File 'lib/batch.rb', line 65

def report_error(item, error)
  $stderr.puts "#{item.inspect}: #{error}\n"
end

#report_errorsObject



55
56
57
58
59
60
61
62
63
# File 'lib/batch.rb', line 55

def report_errors
  return if @errors.empty?

  $stderr.puts "\nSome errors occured:\n\n"

  @errors.each do |item, error|
    report_error(item, error)
  end
end

#report_progress(io) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/batch.rb', line 80

def report_progress(io)
  if progress
    io.print "#{progress.to_s.rjust 3, " "}% "
  else
    io.print "   ? "
  end
end

#totalObject



69
70
71
# File 'lib/batch.rb', line 69

def total
  @size
end