diff options
author | Jacopo <beschi.jacopo@gmail.com> | 2018-04-19 12:31:01 +0300 |
---|---|---|
committer | Jacopo <beschi.jacopo@gmail.com> | 2018-04-19 14:56:37 +0300 |
commit | 9000626a60c889c76ff1dfc8c6247f15953ca993 (patch) | |
tree | 0d1c6fd369301b146464634b4360dd79002dac30 /app/models/concerns/uniquify.rb | |
parent | 6ae3098eb8f01406190942e8952866dd9af81dde (diff) |
Moves Uniquify counter in the initializer
Diffstat (limited to 'app/models/concerns/uniquify.rb')
-rw-r--r-- | app/models/concerns/uniquify.rb | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/app/models/concerns/uniquify.rb b/app/models/concerns/uniquify.rb index db51ed2dbeb..549a76da20e 100644 --- a/app/models/concerns/uniquify.rb +++ b/app/models/concerns/uniquify.rb @@ -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 |