Class: KSUID::Type
Overview
Encapsulates the data type for a KSUID
This is the main class that you will interact with in this gem. You will not typically generate these directly, but this is the resulting data type for all of the main generation methods on the KSUID module.
A KSUID type has two pieces of information contained within its byte-encoded data:
-
The timestamp associated with the KSUID (stored as the first 4 bytes)
-
The payload, or random data, for the KSUID (stored as the last 16 bytes)
The type gives you access to several handles into these data.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
private
Implements the Comparable interface for sorting KSUIDs.
-
#==(other) ⇒ Boolean
Checks whether this KSUID is equal to another.
-
#eql?(other) ⇒ Boolean
Checks whether this KSUID hashes to the same hash key as another.
-
#hash ⇒ Integer
Generates the key to use when using a KSUID as a hash key.
-
#initialize(payload: nil, time: Time.now) ⇒ KSUID::Type
constructor
Instantiates a new KSUID type.
-
#inspect ⇒ String
Prints the KSUID for debugging within a console.
-
#payload ⇒ String
The payload for the KSUID, as a hex-encoded string.
-
#raw ⇒ String
The KSUID as a hex-encoded string.
-
#to_bytes ⇒ String
The KSUID as a byte string.
-
#to_i ⇒ Integer
The KSUID as a Unix timestamp.
-
#to_s ⇒ String
The KSUID as a base 62-encoded string.
-
#to_time ⇒ String
The time the KSUID was generated.
Constructor Details
#initialize(payload: nil, time: Time.now) ⇒ KSUID::Type
Instantiates a new KSUID type
36 37 38 39 40 41 |
# File 'lib/ksuid/type.rb', line 36 def initialize(payload: nil, time: Time.now) payload ||= KSUID.config.random_generator.call byte_encoding = Utils.int_to_bytes(time.to_i - EPOCH_TIME) @uid = byte_encoding.bytes + payload.bytes end |
Instance Method Details
#<=>(other) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Implements the Comparable interface for sorting KSUIDs
49 50 51 |
# File 'lib/ksuid/type.rb', line 49 def <=>(other) to_time <=> other.to_time end |
#==(other) ⇒ Boolean
Checks whether this KSUID is equal to another
62 63 64 |
# File 'lib/ksuid/type.rb', line 62 def ==(other) other.to_s == to_s end |
#eql?(other) ⇒ Boolean
Checks whether this KSUID hashes to the same hash key as another
75 76 77 |
# File 'lib/ksuid/type.rb', line 75 def eql?(other) hash == other.hash end |
#hash ⇒ Integer
Generates the key to use when using a KSUID as a hash key
92 93 94 |
# File 'lib/ksuid/type.rb', line 92 def hash @uid.hash end |
#inspect ⇒ String
Prints the KSUID for debugging within a console
104 105 106 |
# File 'lib/ksuid/type.rb', line 104 def inspect "<KSUID(#{self})>" end |
#payload ⇒ String
The payload for the KSUID, as a hex-encoded string
This is generally useful for comparing against the Go tool
120 121 122 |
# File 'lib/ksuid/type.rb', line 120 def payload Utils.bytes_to_hex_string(uid.last(BYTES[:payload])) end |
#raw ⇒ String
The KSUID as a hex-encoded string
This is generally useful for comparing against the Go tool.
136 137 138 |
# File 'lib/ksuid/type.rb', line 136 def raw Utils.bytes_to_hex_string(uid) end |
#to_bytes ⇒ String
The KSUID as a byte string
150 151 152 |
# File 'lib/ksuid/type.rb', line 150 def to_bytes Utils.byte_string_from_array(uid) end |
#to_i ⇒ Integer
The KSUID as a Unix timestamp
164 165 166 |
# File 'lib/ksuid/type.rb', line 164 def to_i Utils.int_from_bytes(uid.first(BYTES[:timestamp])) end |
#to_s ⇒ String
The KSUID as a base 62-encoded string
178 179 180 |
# File 'lib/ksuid/type.rb', line 178 def to_s Base62.encode_bytes(uid) end |
#to_time ⇒ String
The time the KSUID was generated
192 193 194 |
# File 'lib/ksuid/type.rb', line 192 def to_time Time.at(to_i + EPOCH_TIME) end |