Moves Uniquify counter in the initializer
This commit is contained in:
parent
6ae3098eb8
commit
9000626a60
|
@ -1,16 +1,21 @@
|
|||
# Uniquify
|
||||
#
|
||||
# Return a version of the given 'base' string that is unique
|
||||
# by appending a counter to it. Uniqueness is determined by
|
||||
# repeated calls to the passed block.
|
||||
#
|
||||
# You can pass an initial value for the counter, if not given
|
||||
# counting starts from 1.
|
||||
#
|
||||
# If `base` is a function/proc, we expect that calling it with a
|
||||
# candidate counter returns a string to test/return.
|
||||
class Uniquify
|
||||
# Return a version of the given 'base' string that is unique
|
||||
# by appending a counter to it. Uniqueness is determined by
|
||||
# repeated calls to the passed block.
|
||||
#
|
||||
# You can pass an initial value for the counter, if not given
|
||||
# counting starts from 1.
|
||||
#
|
||||
# If `base` is a function/proc, we expect that calling it with a
|
||||
# candidate counter returns a string to test/return.
|
||||
def string(base, counter = nil)
|
||||
@base = base
|
||||
def initialize(counter = nil)
|
||||
@counter = counter
|
||||
end
|
||||
|
||||
def string(base)
|
||||
@base = base
|
||||
|
||||
increment_counter! while yield(base_string)
|
||||
base_string
|
||||
|
|
|
@ -198,7 +198,7 @@ class Issue < ActiveRecord::Base
|
|||
return to_branch_name unless project.repository.branch_exists?(to_branch_name)
|
||||
|
||||
start_counting_from = 2
|
||||
Uniquify.new.string(-> (counter) { "#{to_branch_name}-#{counter}" }, start_counting_from) do |suggested_branch_name|
|
||||
Uniquify.new(start_counting_from).string(-> (counter) { "#{to_branch_name}-#{counter}" }) do |suggested_branch_name|
|
||||
project.repository.branch_exists?(suggested_branch_name)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -24,7 +24,9 @@ describe Uniquify do
|
|||
|
||||
it 'allows to pass an initial value for the counter' do
|
||||
start_counting_from = 2
|
||||
result = uniquify.string('test_string', start_counting_from) { |s| s == 'test_string' }
|
||||
uniquify = described_class.new(start_counting_from)
|
||||
|
||||
result = uniquify.string('test_string') { |s| s == 'test_string' }
|
||||
|
||||
expect(result).to eq('test_string2')
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue