Class: TypeID::UUID
- Inherits:
-
String
- Object
- String
- TypeID::UUID
- Defined in:
- lib/typeid/uuid.rb,
lib/typeid/uuid/base32.rb
Overview
Represents a UUID. Can be treated as a string.
Defined Under Namespace
Modules: Base32
Instance Attribute Summary collapse
- #bytes ⇒ Array<Integer> readonly
Class Method Summary collapse
-
.from_base32(string) ⇒ TypeID::UUID
Parses a
UUID
from a base32String
. -
.from_string(string) ⇒ TypeID::UUID
Parses a
UUID
from a rawString
. -
.generate(timestamp: self.class.timestamp) ⇒ TypeID::UUID
Generates a new
UUID
, using gem “uuid7”. -
.timestamp ⇒ Integer
Utility method to generate a timestamp as milliseconds since the Unix epoch.
Instance Method Summary collapse
-
#base32 ⇒ String
Returns the
UUID
encoded as a base32String
. -
#initialize(bytes) ⇒ UUID
constructor
Initializes a
UUID
from an array of bytes. - #inspect ⇒ String
-
#timestamp ⇒ Integer
Returns the timestamp of the
UUID
as milliseconds since the Unix epoch.
Constructor Details
#initialize(bytes) ⇒ UUID
Initializes a UUID
from an array of bytes.
50 51 52 53 54 |
# File 'lib/typeid/uuid.rb', line 50 def initialize(bytes) @bytes = bytes super(string) end |
Instance Attribute Details
#bytes ⇒ Array<Integer> (readonly)
8 9 10 |
# File 'lib/typeid/uuid.rb', line 8 def bytes @bytes end |
Class Method Details
.from_base32(string) ⇒ TypeID::UUID
Parses a UUID
from a base32 String
.
29 30 31 |
# File 'lib/typeid/uuid.rb', line 29 def self.from_base32(string) new(TypeID::UUID::Base32.decode(string)) end |
.from_string(string) ⇒ TypeID::UUID
Parses a UUID
from a raw String
.
37 38 39 40 41 42 43 44 45 |
# File 'lib/typeid/uuid.rb', line 37 def self.from_string(string) bytes = string .tr("-", "") .chars .each_slice(2) .map { |pair| pair.join.to_i(16) } new(bytes) end |
.generate(timestamp: self.class.timestamp) ⇒ TypeID::UUID
Generates a new UUID
, using gem “uuid7”.
21 22 23 |
# File 'lib/typeid/uuid.rb', line 21 def self.generate(timestamp: self.class.) from_string(UUID7.generate(timestamp: )) end |
.timestamp ⇒ Integer
Utility method to generate a timestamp as milliseconds since the Unix epoch.
13 14 15 |
# File 'lib/typeid/uuid.rb', line 13 def self. Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond) end |
Instance Method Details
#base32 ⇒ String
Returns the UUID
encoded as a base32 String
.
59 60 61 |
# File 'lib/typeid/uuid.rb', line 59 def base32 TypeID::UUID::Base32.encode(bytes) end |
#inspect ⇒ String
73 74 75 |
# File 'lib/typeid/uuid.rb', line 73 def inspect "#<#{self.class.name} #{to_s}>" end |
#timestamp ⇒ Integer
Returns the timestamp of the UUID
as milliseconds since the Unix epoch.
66 67 68 69 70 |
# File 'lib/typeid/uuid.rb', line 66 def bytes[0..5] .map.with_index { |byte, index| byte << (5 - index) * 8 } .inject(:|) end |