Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spec case updates #17

Merged
merged 7 commits into from
Apr 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix specs
  • Loading branch information
broothie committed Apr 13, 2024
commit 8fd394cf051c04a7ca95aa560a4430a6fcf7b775
12 changes: 8 additions & 4 deletions lib/typeid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ class Error < StandardError; end
# @param string [String] string representation of a +TypeID+
# @return [TypeID]
def self.from_string(string)
case string.split("_")
in [suffix]
result = string.rpartition("_")
puts result

case string.rpartition("_")
in ["", "", suffix]
from("", suffix)

in [prefix, suffix]
in [prefix, "_", suffix]
raise Error, "prefix cannot be empty when there's a separator" if prefix.empty?

from(prefix, suffix)
Expand Down Expand Up @@ -83,7 +86,8 @@ def initialize(
suffix: TypeID::UUID.generate(timestamp: timestamp).base32
)
raise Error, "prefix length cannot be greater than #{MAX_PREFIX_LENGTH}" if prefix.length > MAX_PREFIX_LENGTH
raise Error, "prefix must be lowercase ASCII characters" unless prefix.match?(/^[a-z]*$/)
raise Error, "prefix must be lowercase ASCII characters" unless prefix.match?(/^[a-z_]*$/)
raise Error, "prefix cannot start or end with an underscore" if prefix.start_with?("_") || prefix.end_with?("_")
raise Error, "suffix must be #{TypeID::UUID::Base32::ENCODED_STRING_LENGTH} characters" unless suffix.length == TypeID::UUID::Base32::ENCODED_STRING_LENGTH
raise Error, "suffix must only contain the letters in '#{TypeID::UUID::Base32::ALPHABET}'" unless suffix.chars.all? { |char| TypeID::UUID::Base32::ALPHABET.include?(char) }
raise Error, "suffix must start with a 0-7 digit to avoid overflows" unless ("0".."7").cover?(suffix.chars.first)
Expand Down
Loading