Class: Hegel::Generators::UuidsGenerator

Inherits:
Hegel::Generator show all
Defined in:
lib/hegel/generators.rb,
sig/hegel.rbs

Overview

Hegel::Syntax::Methods#uuids. A UUID String in the standard 8-4-4-4-12 hex form, matching SecureRandom.uuid's own format. It returns a String because Ruby's stdlib has no dedicated UUID type and SecureRandom itself returns one; a richer type would mean a runtime dependency, which this gem does not take for a formatting choice. version: nil (the default) draws uniform random bits except the nil UUID, per the header; an explicit version forces the RFC 4122 version and variant nibbles.

Opens no span: HEGEL_LABEL_UUID is a per-draw label the engine itself emits inside hegel_generate_uuid, not something this binding opens -- the header's own comment on HEGEL_LABEL_INTEGER says the same of hegel_generate_integer/_big ("Emitted internally, like every per-draw label"). BooleanGenerator, IntegerGenerator, and FloatGenerator each make exactly one native call to produce their own value the same way uuids() does here, and each opens no span of its own for the same reason. A span belongs only around a generator that composes more than one native call into one draw (see IpAddressesGenerator, whose family choice and address draw are two calls under one span).

Instance Method Summary collapse

Methods inherited from Hegel::Generator

#filter, #map

Constructor Details

#initialize(version:) ⇒ UuidsGenerator

Returns a new instance of UuidsGenerator.

Parameters:

  • version: (Integer, nil)


590
591
592
593
# File 'lib/hegel/generators.rb', line 590

def initialize(version:)
  super()
  @version = version
end

Instance Method Details

#do_draw(tc) ⇒ String

Parameters:

  • tc (Object)

Returns:

  • (String)


595
596
597
598
599
600
# File 'lib/hegel/generators.rb', line 595

def do_draw(tc)
  has_version = !@version.nil?
  raw = tc.generate_uuid(@version || 0, has_version)
  hex = raw.unpack1("H*")
  "#{hex[0, 8]}-#{hex[8, 4]}-#{hex[12, 4]}-#{hex[16, 4]}-#{hex[20, 12]}"
end