Module: ULID

Includes:
Constants
Defined in:
lib/ulid.rb,
lib/ulid/parse.rb,
lib/ulid/compare.rb,
lib/ulid/version.rb,
lib/ulid/generate.rb,
lib/ulid/constants.rb,
lib/ulid/identifier.rb

Defined Under Namespace

Modules: Compare, Constants, Generate, Parse Classes: Identifier

Constant Summary collapse

VERSION =
"1.0.2"

Constants included from Constants

Constants::B32REF, Constants::ENCODING, Constants::MAX_ENTROPY, Constants::MAX_TIME, Constants::MIN_ENTROPY, Constants::MIN_TIME

Class Method Summary collapse

Class Method Details

.at(at_time) ⇒ String

Get a string ULID encoding the given time.

Examples:

Generate a ULID string for a given time

ULID.at(Time.at(1_000_000)) #=> 0000XSNJG0H890HQE70THPQ3D3

Parameters:

  • at_time (Time)

    the time to encode

Returns:

  • (String)

    new ULID string encoding the given time



45
46
47
# File 'lib/ulid.rb', line 45

def self.at(at_time)
  Identifier.new(at_time).ulid
end

.generateString

Get a new, randomized ULID string at the current time.

Examples:

Generate a ULID string

ULID.generate #=> 0DHWZV7PGEAFYDYH04X7Y468GQ

Returns:

  • (String)

    26 character ULID value for the current system time



33
34
35
# File 'lib/ulid.rb', line 33

def self.generate
  Identifier.new.ulid
end

.max_ulid_at(at_time) ⇒ String

Get the first possible ULID string for the given time in sort order descending.

Examples:

Get minimal ULID at time

ULID.max_ulid_at Time.at(1_000_000) #=> "0000XSNJG0ZZZZZZZZZZZZZZZZ"

Parameters:

  • at_time (Time)

    a Time value to encode in the ULID

Returns:

  • (String)

    the lexicographically maximum ULID value for the given time



81
82
83
# File 'lib/ulid.rb', line 81

def self.max_ulid_at(at_time)
  Identifier.new(at_time, MAX_ENTROPY).ulid
end

.min_ulid_at(at_time) ⇒ String

Get the first possible ULID string for the given time in sort order ascending.

Examples:

Get minimal ULID at time

ULID.min_ulid_at Time.at(1_000_000) #=> "0000XSNJG00000000000000000"

Parameters:

  • at_time (Time)

    a Time value to encode in the ULID

Returns:

  • (String)

    the lexicographically minimum ULID value for the given time



69
70
71
# File 'lib/ulid.rb', line 69

def self.min_ulid_at(at_time)
  Identifier.new(at_time, MIN_ENTROPY).ulid
end

.new(*args) ⇒ ULID::Identifier

Get a new, randomized ULID::Identifier instance at the current time.

Examples:

Generate a ULID

ULID.new #=> #<ULID::Identifier:0x007f83f90aecc0 ....>

Returns:

  • (ULID::Identifier)

    new ULID::Identifier instance for the current system time



22
23
24
# File 'lib/ulid.rb', line 22

def self.new(*args)
  Identifier.new(*args)
end

.time(ulid) ⇒ Time

Get the Time value encoded by the given ULID string.

Examples:

Parse a ULID string and get a time value

ULID.time '0009A0QS00SGEFTGMFFEAS6B9A' #=> 1970-04-26 17:46:40 UTC

Parameters:

Returns:

  • (Time)

    UTC time value encoded by the ULID



57
58
59
# File 'lib/ulid.rb', line 57

def self.time(ulid)
  Identifier.new(ulid).time.utc
end