diff options
Diffstat (limited to 'app/models/concerns/uniquify.rb')
-rw-r--r-- | app/models/concerns/uniquify.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/app/models/concerns/uniquify.rb b/app/models/concerns/uniquify.rb new file mode 100644 index 00000000000..a7fe5951b6e --- /dev/null +++ b/app/models/concerns/uniquify.rb @@ -0,0 +1,30 @@ +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. + # + # If `base` is a function/proc, we expect that calling it with a + # candidate counter returns a string to test/return. + def string(base) + @base = base + @counter = nil + + increment_counter! while yield(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 ||= 0 + @counter += 1 + end +end |