batch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Michel Martens & Damian Janowski
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ Batch
2
+ =====
3
+
4
+ `<%= Faker::Company.bs %>`
5
+
6
+ Usage
7
+ -----
8
+
9
+ require "batch"
10
+
11
+ Batch.each(Model.all) do |model|
12
+ # do something with model
13
+ # and see the overall progress
14
+ end
15
+
16
+ Given that `Model.all` responds to `each` and `size`, you'll get a nice
17
+ progress report:
18
+
19
+ 0% ...........................................................................
20
+ 25% ...........................................................................
21
+ 50% ...........................................................................
22
+ 75% ...........................................................................
23
+ 100%
24
+
25
+ If errors occur, they are handled so that your long-running scripts
26
+ don't get interrupted right after you go to bed:
27
+
28
+ 0% .......E...................................................................
29
+ 25% ..........................................E................................
30
+ 50% ....................E......................................................
31
+ 75% ...........................................................................
32
+ 100%
33
+
34
+ Some errors occured:
35
+
36
+ # ... detailed exceptions here
@@ -0,0 +1,7 @@
1
+ require "rake/testtask"
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.test_files = FileList["test/**/*_test.rb"]
5
+ end
6
+
7
+ task :default => :test
@@ -0,0 +1,9 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "batch"
3
+ s.version = "0.0.1"
4
+ s.summary = "Iterate Enumerables with progress reporting."
5
+ s.authors = ["Damian Janowski", "Michel Martens"]
6
+ s.email = ["djanowski@dimaion.com", "michel@soveran.com"]
7
+ s.homepage = "http://github.com/djanowski/batch"
8
+ s.files = ["LICENSE", "README.markdown", "Rakefile", "lib/batch.rb", "batch.gemspec", "test/batch_test.rb", "test/test_helper.rb"]
9
+ end
@@ -0,0 +1,68 @@
1
+ class Batch
2
+ VERSION = "0.0.1"
3
+
4
+ attr :enumerable
5
+
6
+ def initialize(enumerable)
7
+ @enumerable = enumerable
8
+ end
9
+
10
+ def each(&block)
11
+ @errors = []
12
+ @current = 0
13
+
14
+ print " 0% "
15
+
16
+ enumerable.each do |item|
17
+ begin
18
+ yield(item)
19
+ print "."
20
+
21
+ rescue Exception => e
22
+ print "E"
23
+ @errors << [item, e]
24
+
25
+ ensure
26
+ @current += 1
27
+
28
+ report_progress if eol?
29
+ end
30
+ end
31
+
32
+ print "\n"
33
+
34
+ report_errors
35
+
36
+ nil
37
+ end
38
+
39
+ def report_errors
40
+ return if @errors.empty?
41
+
42
+ $stderr.puts "\nSome errors occured:\n\n"
43
+
44
+ @errors.each do |item, error|
45
+ $stderr.puts "#{item.inspect}: #{error}\n"
46
+ end
47
+ end
48
+
49
+ def total
50
+ @enumerable.size
51
+ end
52
+
53
+ def progress
54
+ @current * 100 / total
55
+ end
56
+
57
+ def report_progress
58
+ print "\n#{progress.to_s.rjust 3, " "}% "
59
+ end
60
+
61
+ def eol?
62
+ @current % 75 == 0 || @current == total
63
+ end
64
+
65
+ def self.each(enumerable, &block)
66
+ new(enumerable).each(&block)
67
+ end
68
+ end
@@ -0,0 +1,43 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class BatchTest < Test::Unit::TestCase
4
+ should "report" do
5
+ stdout, _ = capture do
6
+ Batch.each((1..80).to_a) do |item|
7
+ item + 1
8
+ end
9
+ end
10
+
11
+ expected = <<-EOS
12
+ 0% ...........................................................................
13
+ 93% .....
14
+ 100%
15
+ EOS
16
+
17
+ assert_equal expected.rstrip, stdout.rstrip
18
+ end
19
+
20
+ should "not halt on errors" do
21
+ stdout, stderr = capture do
22
+ Batch.each((1..80).to_a) do |item|
23
+ raise ArgumentError, "Oops" if item == 3
24
+ end
25
+ end
26
+
27
+ expected_stdout = <<-EOS
28
+ 0% ..E........................................................................
29
+ 93% .....
30
+ 100%
31
+ EOS
32
+
33
+ expected_stderr = <<-EOS
34
+
35
+ Some errors occured:
36
+
37
+ 3: Oops
38
+ EOS
39
+
40
+ assert_equal expected_stdout.rstrip, stdout.rstrip
41
+ assert_equal expected_stderr.rstrip, stderr.rstrip
42
+ end
43
+ end
@@ -0,0 +1,13 @@
1
+ require "stringio"
2
+ require "contest"
3
+ require File.join(File.dirname(__FILE__), "..", "lib", "batch")
4
+
5
+ def capture
6
+ stdout, $stdout = $stdout, StringIO.new
7
+ stderr, $stderr = $stderr, StringIO.new
8
+ yield
9
+ [$stdout.string, $stderr.string]
10
+ ensure
11
+ $stdout = stdout
12
+ $stderr = stderr
13
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: batch
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Damian Janowski
14
+ - Michel Martens
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-05-10 00:00:00 -03:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description:
24
+ email:
25
+ - djanowski@dimaion.com
26
+ - michel@soveran.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - LICENSE
35
+ - README.markdown
36
+ - Rakefile
37
+ - lib/batch.rb
38
+ - batch.gemspec
39
+ - test/batch_test.rb
40
+ - test/test_helper.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/djanowski/batch
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7.pre.1
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Iterate Enumerables with progress reporting.
75
+ test_files: []
76
+