Class: Batch
- Inherits:
-
Object
- Object
- Batch
- Defined in:
- lib/batch.rb
Constant Summary collapse
- VERSION =
"1.0.3"
Instance Attribute Summary collapse
-
#enumerable ⇒ Object
readonly
Returns the value of attribute enumerable.
Class Method Summary collapse
- .each(enumerable, &block) ⇒ Object
- .interactive? ⇒ Boolean
- .out(&block) ⇒ Object
- .start(title, enumerable, &block) ⇒ Object
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #eol? ⇒ Boolean
-
#initialize(enumerable) ⇒ Batch
constructor
A new instance of Batch.
- #progress ⇒ Object
- #report_error(item, error) ⇒ Object
- #report_errors ⇒ Object
- #report_progress(io) ⇒ Object
- #total ⇒ Object
Constructor Details
#initialize(enumerable) ⇒ Batch
Returns a new instance of Batch.
6 7 8 9 10 |
# 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) end |
Instance Attribute Details
#enumerable ⇒ Object (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
91 92 93 |
# File 'lib/batch.rb', line 91 def self.each(enumerable, &block) new(enumerable).each(&block) end |
.interactive? ⇒ Boolean
115 116 117 |
# File 'lib/batch.rb', line 115 def self.interactive? (ENV["BATCH_INTERACTIVE"] || 1).to_i == 1 end |
.out(&block) ⇒ Object
111 112 113 |
# File 'lib/batch.rb', line 111 def self.out(&block) interactive? ? yield($stdout) : File.open("/dev/null", "w", &block) end |
.start(title, enumerable, &block) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/batch.rb', line 95 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
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/batch.rb', line 12 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
87 88 89 |
# File 'lib/batch.rb', line 87 def eol? @current % @width == 0 end |
#progress ⇒ Object
72 73 74 75 76 77 |
# File 'lib/batch.rb', line 72 def progress return unless @size return 0 if @size == 0 @current * 100 / @size end |
#report_error(item, error) ⇒ Object
64 65 66 |
# File 'lib/batch.rb', line 64 def report_error(item, error) $stderr.puts "#{item.inspect}: #{error}\n" end |
#report_errors ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/batch.rb', line 54 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
79 80 81 82 83 84 85 |
# File 'lib/batch.rb', line 79 def report_progress(io) if progress io.print "#{progress.to_s.rjust 3, " "}% " else io.print " ? " end end |
#total ⇒ Object
68 69 70 |
# File 'lib/batch.rb', line 68 def total @size end |