Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Andrew <mail@timothyandrew.net>2017-02-03 15:55:50 +0300
committerTimothy Andrew <mail@timothyandrew.net>2017-02-24 14:20:19 +0300
commitca16c3734b7b89f71bdc9e1c18152aa1599c4f89 (patch)
treec00010e64f210675c3a6c777f080aae51455934b /app/models/concerns/uniquify.rb
parentff19bbd3b40621ae94632b9aa68fd12645b6ed41 (diff)
Extract code from `Namespace#clean_path` for ghost user generation.
1. Create a `Uniquify` class, which generalizes the process of generating unique strings, by accepting a function that defines what "uniqueness" means in a given context. 2. WIP: Make sure tests for `Namespace` pass, add more if necessary. 3. WIP: Add tests for `Uniquify`
Diffstat (limited to 'app/models/concerns/uniquify.rb')
-rw-r--r--app/models/concerns/uniquify.rb25
1 files changed, 25 insertions, 0 deletions
diff --git a/app/models/concerns/uniquify.rb b/app/models/concerns/uniquify.rb
new file mode 100644
index 00000000000..1485ab6ae99
--- /dev/null
+++ b/app/models/concerns/uniquify.rb
@@ -0,0 +1,25 @@
+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 `exists_fn`.
+ #
+ # 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)
+ @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
+ end
+
+ private
+
+ def increment_counter!
+ @counter = @counter ? @counter.next : 1
+ end
+end