Class: Graphomaton::IdentifierAllocator
- Inherits:
-
Object
- Object
- Graphomaton::IdentifierAllocator
- Defined in:
- lib/graphomaton/identifier_allocator.rb
Overview
Assigns deterministic, collision-free identifiers within one export.
Instance Method Summary collapse
- #allocate(key, preferred: nil, prefix: 'id') ⇒ Object
-
#initialize(reserved: []) ⇒ IdentifierAllocator
constructor
A new instance of IdentifierAllocator.
Constructor Details
#initialize(reserved: []) ⇒ IdentifierAllocator
Returns a new instance of IdentifierAllocator.
6 7 8 9 10 |
# File 'lib/graphomaton/identifier_allocator.rb', line 6 def initialize(reserved: []) @identifiers = {} @used = reserved.to_h { |identifier| [identifier.to_s, true] } @counters = Hash.new(0) end |
Instance Method Details
#allocate(key, preferred: nil, prefix: 'id') ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/graphomaton/identifier_allocator.rb', line 12 def allocate(key, preferred: nil, prefix: 'id') return @identifiers[key] if @identifiers.key?(key) candidate = preferred.to_s unless preferred.nil? candidate = nil if candidate&.empty? || @used.key?(candidate) candidate ||= next_identifier(prefix) @used[candidate] = true @identifiers[key] = candidate end |