Skip to content

Commit

Permalink
Implement encode & decode methods in the main module
Browse files Browse the repository at this point in the history
  • Loading branch information
pch committed Dec 6, 2023
1 parent 9f3d06b commit 6212432
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -65,14 +65,23 @@ 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
based_uuid
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
Expand Down
7 changes: 6 additions & 1 deletion lib/based_uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/based_uuid/has_based_uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/based_uuid/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module BasedUUID
VERSION = "0.5.2".freeze
VERSION = "0.6.0".freeze
end
16 changes: 12 additions & 4 deletions test/test_based_uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6212432

Please sign in to comment.