From 6212432c7744485be19bf469ba7c36ef786ee333 Mon Sep 17 00:00:00 2001 From: Piotr Chmolowski Date: Wed, 6 Dec 2023 16:07:18 +0100 Subject: [PATCH] Implement encode & decode methods in the main module --- README.md | 13 +++++++++++-- lib/based_uuid.rb | 7 ++++++- lib/based_uuid/has_based_uuid.rb | 2 +- lib/based_uuid/version.rb | 2 +- test/test_based_uuid.rb | 16 ++++++++++++---- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 1516407..8b6cbb4 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ bpo_12dm1qresn83st62reqdw7f7cv #=> 226d037c-3b35-40f3-a30b-0ebb78779d9b This gem encodes UUID primary keys into 26-character lowercase strings using [Crockford’s base32](https://www.crockford.com/base32.html) encoding. The optional prefix helps you identify the model it represents. -BasedUUID assumes that you have a [UUID primary key](https://guides.rubyonrails.org/v5.0/active_record_postgresql.html#uuid) (`id`) in your ActiveRecord model. It doesn’t affect how your ActiveRecord primary key UUIDs are stored in the database. Prefix and base32 encoding are only used for presentation. +BasedUUID assumes that you have a [UUID primary key](https://guides.rubyonrails.org/v5.0/active_record_postgresql.html#uuid) (`id`) in your ActiveRecord model. It doesn’t affect how your primary key UUIDs are stored in the database. Prefixes and base32-encoded strings are only used for presentation. ## Installation @@ -65,7 +65,7 @@ BasedUUID.find("user_763j02ryxh8dbs56mgcjqrmmgt") ### BasedUUID as default URL identifiers -BasedUUID aims to be unintrusive and it doesn’t affect how Rails URLs are generated, so if you want to use it as default URL param, add this to your model: +BasedUUID aims to be non-intrusive and it doesn’t affect how Rails URLs are generated, so if you want to use it as default URL param, add this to your model: ```ruby def to_param @@ -73,6 +73,15 @@ def to_param end ``` +### Use outside ActiveRecord + +BasedUUID can be used outside ActiveRecord, too. You encode any UUID with it: + +```ruby +BasedUUID.encode(uuid: "226d037c-3b35-40f3-a30b-0ebb78779d9b", prefix: :bpo) +BasedUUID.decode("bpo_12dm1qresn83st62reqdw7f7cv") +``` + * * * ## Development diff --git a/lib/based_uuid.rb b/lib/based_uuid.rb index 6378bd6..957bdb9 100644 --- a/lib/based_uuid.rb +++ b/lib/based_uuid.rb @@ -30,11 +30,16 @@ def split(token) [prefix.presence, uuid_base32] end - def based_uuid(uuid:, prefix:) + def encode(uuid:, prefix:) uuid_base32 = Base32UUID.encode(uuid) return uuid_base32 unless prefix "#{prefix}#{delimiter}#{uuid_base32}" end + + def decode(token) + _, uuid_base32 = split(token) + Base32UUID.decode(uuid_base32) + end end end diff --git a/lib/based_uuid/has_based_uuid.rb b/lib/based_uuid/has_based_uuid.rb index cf39955..fee3a6d 100644 --- a/lib/based_uuid/has_based_uuid.rb +++ b/lib/based_uuid/has_based_uuid.rb @@ -41,7 +41,7 @@ def find_by_based_uuid!(token) def based_uuid(prefix: true) raise ArgumentError, "UUID is empty" if _primary_key_value.blank? - BasedUUID.based_uuid(uuid: _primary_key_value, prefix: prefix ? self.class._based_uuid_prefix : nil) + BasedUUID.encode(uuid: _primary_key_value, prefix: prefix ? self.class._based_uuid_prefix : nil) end private diff --git a/lib/based_uuid/version.rb b/lib/based_uuid/version.rb index a4b57b3..8595e65 100644 --- a/lib/based_uuid/version.rb +++ b/lib/based_uuid/version.rb @@ -1,3 +1,3 @@ module BasedUUID - VERSION = "0.5.2".freeze + VERSION = "0.6.0".freeze end diff --git a/test/test_based_uuid.rb b/test/test_based_uuid.rb index 2546e99..44f550a 100644 --- a/test/test_based_uuid.rb +++ b/test/test_based_uuid.rb @@ -12,16 +12,24 @@ def test_that_it_has_a_version_number refute_nil ::BasedUUID::VERSION end - def test_based_uuid + def test_encoding uuid = "018c3b49-560c-719f-9daf-bae405f6ffca" base32 = "01hgxmjngce6fsvbxtwg2zdzya" - assert_equal "example_#{base32}", BasedUUID.based_uuid(uuid:, prefix: "example") - assert_equal base32, BasedUUID.based_uuid(uuid:, prefix: nil) + assert_equal "example_#{base32}", BasedUUID.encode(uuid:, prefix: "example") + assert_equal base32, BasedUUID.encode(uuid:, prefix: nil) BasedUUID.delimiter = "-" - assert_equal "example-#{base32}", BasedUUID.based_uuid(uuid:, prefix: "example") + assert_equal "example-#{base32}", BasedUUID.encode(uuid:, prefix: "example") + end + + def test_decoding + base32 = "01hgxmjngce6fsvbxtwg2zdzya" + + assert_equal "018c3b49-560c-719f-9daf-bae405f6ffca", BasedUUID.decode(base32) + assert_equal "018c3b49-560c-719f-9daf-bae405f6ffca", BasedUUID.decode("example_#{base32}") + assert_equal "018c3b49-560c-719f-9daf-bae405f6ffca", BasedUUID.decode("anyprefixwillworkhere_#{base32}") end def test_splitting_tokens_with_prefixes