Skip to content

Commit 2616f99

Browse files
committed
Add: Ability To Access Progress Bar Instance Using Enumerator Refinement
Why This Change Is Necessary ======================================================================== Without access to the progress bar instance, when using the refinement there's a lack of ability to do things like log or modify the title mid-progress like you can with the standard progress bar usage. What These Changes Do To Address the Issue ======================================================================== If you pass a second argument to the block passed to `with_progressbar`, the second argument will be the progress bar instance. Side Effects Caused By This Change ======================================================================== None expected. ------------------------------------------------------------------------ **Relevant URLs:** * [Current Ticket Link][1] * [Original Ticket Link][2] **Actions:** * Closes 184 * Closes 185 ------------------------------------------------------------------------ [1]: #185 [2]: #184
1 parent 9e58074 commit 2616f99

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

lib/ruby-progressbar/refinements/enumerator.rb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
class ProgressBar
22
module Refinements
33
module Enumerator
4-
refine ::Enumerator do
5-
def with_progressbar(options = {}, &block)
6-
progress_bar = ProgressBar.create(options.merge(:starting_at => 0, :total => size))
4+
ARITY_ERROR_MESSAGE = 'Only two arguments allowed to be passed to ' \
5+
'with_progressbar (item, progress_bar)'.freeze
76

8-
each do |item|
9-
progress_bar.increment
7+
refine ::Enumerator do
8+
def with_progressbar(options = {}, &block)
9+
progress_bar = ProgressBar.create(options.merge(:starting_at => 0, :total => size))
1010

11-
next unless block
11+
each do |item|
12+
progress_bar.increment
1213

13-
yielded_args = []
14-
yielded_args << item
14+
next unless block
1515

16-
yield(*yielded_args)
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
1724
end
1825
end
1926
end
2027
end
2128
end
22-
end

spec/lib/ruby-progressbar/refinements/enumerator_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)