Skip to content

Commit d7a2a51

Browse files
committed
Merge branch 'refinement-changes'
2 parents 767ee97 + ab66d73 commit d7a2a51

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
require 'ruby-progressbar/refinements/enumerator'
1+
require 'ruby-progressbar/refinements/progress_enumerator'

lib/ruby-progressbar/refinements/enumerator.rb

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class ProgressBar
2+
module Refinements
3+
module Enumerator
4+
ARITY_ERROR_MESSAGE = 'Only two arguments allowed to be passed to ' \
5+
'with_progressbar (item, progress_bar)'.freeze
6+
7+
refine ::Enumerator do
8+
def with_progressbar(options = {}, &block)
9+
progress_bar = ProgressBar.create(options.merge(:starting_at => 0, :total => size))
10+
11+
each do |item|
12+
progress_bar.increment
13+
14+
next unless block
15+
16+
yielded_args = []
17+
yielded_args << item if block.arity > 0
18+
yielded_args << progress_bar if block.arity > 1
19+
20+
fail ::ArgumentError, ARITY_ERROR_MESSAGE if block.arity > 2
21+
22+
yield(*yielded_args)
23+
end
24+
end
25+
end
26+
end
27+
end
28+
end

spec/lib/ruby-progressbar/refinements/enumerator_spec.rb renamed to spec/lib/ruby-progressbar/refinements/progress_enumerator_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
if Module.private_instance_methods.include?(:using)
22

33
require 'spec_helper'
4-
require 'ruby-progressbar/refinements/enumerator'
4+
require 'ruby-progressbar/refinements/progress_enumerator'
55

66
class ProgressBar
77
module Refinements
@@ -84,6 +84,16 @@ module Refinements
8484

8585
expect(enumerator.map(&transform)).to eql((0...10).map(&transform))
8686
end
87+
88+
it 'passes the progressbar instance to the block when two arguments are requested for the block' do
89+
progress = 0
90+
current_item = -1
91+
92+
(0...10).each.with_progressbar do |item, progress_bar|
93+
expect(progress_bar.progress).to be(progress += 1)
94+
expect(item).to be(current_item += 1)
95+
end
96+
end
8797
end
8898
end
8999
end

0 commit comments

Comments
 (0)