Module: SimpleIdentifierGenerator

Extended by:
SimpleIdentifierGenerator
Included in:
SimpleIdentifierGenerator
Defined in:
lib/xcode/simple_identifier_generator.rb

Constant Summary collapse

MAX_IDENTIFIER_GENERATION_ATTEMPTS =
10

Instance Method Summary collapse

Instance Method Details

#generate(options = {}) ⇒ String

Generate an identifier string

Examples:

generating a new identifier


SimpleIdentifierGenerator.generate # => "E21EB9EE14E359840058122A"

generating a new idenitier ensuring it does not match existing keys


SimpleIdentifierGenerator.generate :existing_keys => [ "E21EB9EE14E359840058122A" ] # => "2574D65B0D2FFDB5D0372B4A"

Parameters:

  • options (Hash) (defaults to: {})

    contains any additional parameters; namely the ‘:existing_keys` parameters which will help ensure uniqueness.

Returns:

  • (String)

    a 24-length string that contains only hexadecimal characters.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/xcode/simple_identifier_generator.rb', line 22

def generate(options = {})
  
  existing_keys = options[:existing_keys] || []
  
  # Ensure that the identifier generated is unique
  identifier_generation_count = 0
  
  new_identifier = generate_new_key
  
  while existing_keys.include?(new_identifier)
    
    new_identifier = generate_new_key
    
    # Increment our identifier generation count and if we reach our max raise
    # an exception as something has gone horribly wrong.

    identifier_generation_count += 1
    if identifier_generation_count > MAX_IDENTIFIER_GENERATION_ATTEMPTS
      raise "SimpleIdentifierGenerator is unable to generate a unique identifier"
    end
  end
  
  new_identifier
  
end