提交 9000626a 编写于 作者: J Jacopo

Moves Uniquify counter in the initializer

上级 6ae3098e
# 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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册