Class: KSUID::Prefixed
- Includes:
- Comparable
- Defined in:
- lib/ksuid/prefixed.rb
Overview
Encapsulates the data type for a prefixed KSUID
When you have different types of KSUIDs in your application, it can be helpful to add an identifier to the front of them to give you an idea for what kind of object the KSUID belongs to.
For example, you might use KSUIDs to identify both Events and Customers. For an Event, you could prefix the KSUID with the string ‘evt_`. Likewise, for Customers, you could prefix them with the string `cus_`.
Prefixed gives you affordances for doing just this.
## Ordering
Prefixeds are partially orderable with Type by their timestamps. When ordering them with other Prefixed instances, they order first by prefix, then by timestamp. This means that in a mixed collection, all Customer KSUIDs (prefix: ‘cus_`) would be grouped before all Event KSUIDs (prefix `evt_`).
## Interface
You typically will not instantiate this class directly, but instead use the prefixed builder method to save some typing.
The most commonly used helper methods for the KSUID module also exist on Prefixed for converting between different forms of output.
## Differences from Type
One other thing to note is that Prefixed is not intended to handle binary data because the prefix does not make sense in either the byte string or packed array formats.
Instance Attribute Summary collapse
-
#prefix ⇒ String
readonly
The prefix in front of the KSUID.
Class Method Summary collapse
-
.call(ksuid, prefix:) ⇒ KSUID::Prefixed
Converts a KSUID-compatible value into a Prefixed.
-
.from_base62(string, prefix:) ⇒ KSUID::Prefixed
Converts a base 62-encoded string into a Prefixed.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
private
Implements the Comparable interface for sorting Prefixeds.
-
#==(other) ⇒ Boolean
Checks whether this Prefixed is equal to another.
-
#hash ⇒ Integer
Generates the key to use when using a Prefixed as a hash key.
-
#initialize(prefix, payload: nil, time: Time.now) ⇒ KSUID::Prefix
constructor
Instantiates a new Prefixed.
-
#raw ⇒ String
The Prefixed as a prefixed, hex-encoded string.
- #to_ksuid ⇒ KSUID::Type
-
#to_s ⇒ String
The Prefixed as a base 62-encoded string.
Methods inherited from Type
#eql?, #inspect, #payload, #to_bytes, #to_i, #to_time
Constructor Details
#initialize(prefix, payload: nil, time: Time.now) ⇒ KSUID::Prefix
Instantiates a new KSUID::Prefixed
129 130 131 132 133 134 135 |
# File 'lib/ksuid/prefixed.rb', line 129 def initialize(prefix, payload: nil, time: Time.now) raise ArgumentError, 'requires a prefix' unless prefix super(payload: payload, time: time) @prefix = prefix end |
Instance Attribute Details
#prefix ⇒ String (readonly)
The prefix in front of the KSUID
146 147 148 |
# File 'lib/ksuid/prefixed.rb', line 146 def prefix @prefix end |
Class Method Details
.call(ksuid, prefix:) ⇒ KSUID::Prefixed
Converts a KSUID-compatible value into a KSUID::Prefixed
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ksuid/prefixed.rb', line 52 def self.call(ksuid, prefix:) return unless ksuid && prefix case ksuid when KSUID::Prefixed then from_base62(ksuid.to_ksuid.to_s, prefix: prefix) when KSUID::Type then from_base62(ksuid.to_s, prefix: prefix) when String then cast_string(ksuid, prefix: prefix) else raise ArgumentError, "Cannot convert #{ksuid.inspect} to KSUID::Prefixed" end end |
.from_base62(string, prefix:) ⇒ KSUID::Prefixed
Converts a base 62-encoded string into a KSUID::Prefixed
74 75 76 77 78 79 80 |
# File 'lib/ksuid/prefixed.rb', line 74 def self.from_base62(string, prefix:) string = string.sub(/\A#{prefix}/, '') int = Base62.decode(string) bytes = Utils.int_to_bytes(int, 160) from_bytes(bytes, prefix: prefix) 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 KSUID::Prefixeds
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/ksuid/prefixed.rb', line 155 def <=>(other) return unless other.is_a?(Type) return super if other.instance_of?(Type) if (result = prefix <=> other.prefix).nonzero? result else super end end |
#==(other) ⇒ Boolean
Checks whether this KSUID::Prefixed is equal to another
175 176 177 178 179 |
# File 'lib/ksuid/prefixed.rb', line 175 def ==(other) other.is_a?(Prefixed) && prefix == other.prefix && super end |
#hash ⇒ Integer
Generates the key to use when using a KSUID::Prefixed as a hash key
194 195 196 |
# File 'lib/ksuid/prefixed.rb', line 194 def hash [prefix, @uid].hash end |
#raw ⇒ String
The KSUID::Prefixed as a prefixed, hex-encoded string
This is generally useful for comparing against the Go tool.
210 211 212 |
# File 'lib/ksuid/prefixed.rb', line 210 def raw super.prepend(prefix) end |
#to_ksuid ⇒ KSUID::Type
Converts the KSUID::Prefixed into a Type by dropping the prefix
224 225 226 |
# File 'lib/ksuid/prefixed.rb', line 224 def to_ksuid KSUID.from_base62(to_s.sub(/\A#{prefix}/, '')) end |
#to_s ⇒ String
The KSUID::Prefixed as a base 62-encoded string
238 239 240 |
# File 'lib/ksuid/prefixed.rb', line 238 def to_s super.prepend(prefix) end |