Class: ZergXcode::IdGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/zerg_xcode/file_format/id_generator.rb

Overview

Generates archive IDs for objects.

Instance Method Summary collapse

Constructor Details

#initializeIdGenerator

Returns a new instance of IdGenerator.



7
8
9
# File 'lib/zerg_xcode/file_format/id_generator.rb', line 7

def initialize
  @assigned_ids = Set.new
end

Instance Method Details

#id_for(object) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/zerg_xcode/file_format/id_generator.rb', line 20

def id_for(object)
  if object.archive_id && !@assigned_ids.include?(object.archive_id)
    @assigned_ids << object.archive_id 
    return object.archive_id 
  else
    return new_id
  end
end

#new_idObject



11
12
13
14
15
16
17
18
# File 'lib/zerg_xcode/file_format/id_generator.rb', line 11

def new_id
  loop do
    id = (0...24).map { '%02X' % rand(256) }.join
    next if @assigned_ids.include? id
    @assigned_ids << id
    return id
  end
end