diff options
author | Timothy Andrew <mail@timothyandrew.net> | 2017-02-07 00:32:35 +0530 |
---|---|---|
committer | Timothy Andrew <mail@timothyandrew.net> | 2017-02-24 16:50:20 +0530 |
commit | 8f01644ff4d25285475cbf053b140292ac50f225 (patch) | |
tree | e2404c12acef20865dabc3197c19b1f17f605d1a /app/models/concerns/uniquify.rb | |
parent | 8e684809765fa866a125c176327825ebc565f5b3 (diff) | |
download | gitlab-ce-8f01644ff4d25285475cbf053b140292ac50f225.tar.gz |
Implement review comments from @rymai and @yorickpeterse
1. Refactoring and specs in the `Uniquify` class.
2. Don't use the `AdvisoryLocking` class. Similar functionality is
provided (backed by Redis) in the `ExclusiveLease` class.
Diffstat (limited to 'app/models/concerns/uniquify.rb')
-rw-r--r-- | app/models/concerns/uniquify.rb | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/app/models/concerns/uniquify.rb b/app/models/concerns/uniquify.rb index 1485ab6ae99..6734472bc6d 100644 --- a/app/models/concerns/uniquify.rb +++ b/app/models/concerns/uniquify.rb @@ -6,19 +6,23 @@ class Uniquify # If `base` is a function/proc, we expect that calling it with a # candidate counter returns a string to test/return. def string(base, exists_fn) + @base = base @counter = nil - if base.respond_to?(:call) - increment_counter! while exists_fn[base.call(@counter)] - base.call(@counter) - else - increment_counter! while exists_fn["#{base}#{@counter}"] - "#{base}#{@counter}" - end + increment_counter! while exists_fn[base_string] + base_string end private + def base_string + if @base.respond_to?(:call) + @base.call(@counter) + else + "#{@base}#{@counter}" + end + end + def increment_counter! @counter = @counter ? @counter.next : 1 end |