Module: Jinx::StringUniquifier

Defined in:
lib/jinx/helpers/string_uniquifier.rb

Overview

A mix-in to convert a String to a unique value.

Constant Summary collapse

CHARS =

The qualifier character range.

'abcdefghijkmnpqrstuvwxyz23456789'

Class Method Summary collapse

Class Method Details

.uniquify(value) ⇒ String?

Returns a relatively unique String for the given base String. Each call returns a distinct value.

This method is useful to transform a String object key to a unique value for testing purposes.

The unique value is comprised of a prefix and suffix. The prefix is the base value with spaces replaced by an underscore. The suffix is given by a UID.generate qualifier converted to digits and lower-case letters, excluding the digits 0, 1 and the characters l, o to avoid confusion.

Examples:

Jinx::StringUniquifier.uniquify('Groucho') #=> Groucho_wiafye6e

Parameters:

  • value (String)

    the value to make unique

Returns:

  • (String, nil)

    the new unique value

Raises:

  • (ArgumentError)

    if value is not a String



23
24
25
26
27
28
29
30
31
32
# File 'lib/jinx/helpers/string_uniquifier.rb', line 23

def self.uniquify(value)
  raise ArgumentError.new("#{value.qp} is not a String") unless String === value
  s = ''
  n = UID.generate
  while n > 0 do
    n, m = n.divmod(32)
    s << CHARS[m]
  end
  [value.gsub(' ', '_'), s].join('_')
end