Skip to content

Commit

Permalink
Add uuid v7 support
Browse files Browse the repository at this point in the history
  • Loading branch information
drexed committed Sep 20, 2024
1 parent 4aa113f commit efaebcf
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 115 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

## [1.5.0] - 2024-09-20
### Added
- Added uuid version option
### Changed
- Ruby send perf improvements

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
lite-uxid (1.4.0)
lite-uxid (1.5.0)

GEM
remote: https://rubygems.org/
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Lite::Uxid.configure do |config|
config.nanoid_size = 21
config.ulid_charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
config.ulid_size = 26
config.uuid_version = 4
end
```

Expand Down Expand Up @@ -92,7 +93,7 @@ Lite::Uxid::Ulid.encode #=> '01GJAY9KGR539EZF4QWYEJGSN7'

## UUID

Implements v4 of the specification. [More information](https://en.wikipedia.org/wiki/Universally_unique_identifier)
Implements `v4` and `v7` of the specification. [More information](https://en.wikipedia.org/wiki/Universally_unique_identifier)

```ruby
Lite::Uxid::Uuid.encode #=> '4376a67e-1189-44b3-a599-7f7566bf105b'
Expand Down
4 changes: 2 additions & 2 deletions lib/lite/uxid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

require "lite/uxid/version"
require "lite/uxid/configuration"
require "lite/uxid/irreversible"
require "lite/uxid/reversible"
require "lite/uxid/base/irreversible"
require "lite/uxid/base/reversible"
require "lite/uxid/record/hashid"
require "lite/uxid/record/nanoid"
require "lite/uxid/record/ulid"
Expand Down
78 changes: 78 additions & 0 deletions lib/lite/uxid/base/irreversible.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# frozen_string_literal: true

require "securerandom" unless defined?(SecureRandom)

module Lite
module Uxid
module Base
class Irreversible

attr_reader :opts

def initialize(opts = {})
@opts = opts
end

class << self

def encode(opts = {})
klass = new(opts)
klass.encode
end

def decode(opts = {})
klass = new(opts)
klass.decode
end

end

def encode
raise NotImplementedError, "override method in #{coder_class}"
end

def decode
raise NotImplementedError, "coder does not support decoding"
end

private

def coder_value_for(key)
sym_key = :"#{coder_class.downcase}_#{key}"
return unless Lite::Uxid.configuration.respond_to?(sym_key)

opts.delete(key) || Lite::Uxid.configuration.send(sym_key)
end

def coder_bytes
@coder_bytes ||= SecureRandom.random_bytes(coder_size).bytes
end

def coder_charset
@coder_charset ||= coder_value_for(:charset)
end

def coder_class
@coder_class ||= self.class.name.split("::").last
end

def coder_length
@coder_length ||= coder_charset.size
end

def coder_salt
@coder_salt ||= coder_value_for(:salt)
end

def coder_size
@coder_size ||= coder_value_for(:size)
end

def coder_version
@coder_version ||= coder_value_for(:version)
end

end
end
end
end
36 changes: 36 additions & 0 deletions lib/lite/uxid/base/reversible.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module Lite
module Uxid
module Base
class Reversible < Irreversible

attr_reader :id

def initialize(id, opts = {})
@id = id
super(opts)
end

class << self

def encode(id, opts = {})
klass = new(id, opts)
klass.encode
end

def decode(id, opts = {})
klass = new(id, opts)
klass.decode
end

end

def decode
raise NotImplementedError, "override method in #{coder_class}"
end

end
end
end
end
6 changes: 5 additions & 1 deletion lib/lite/uxid/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ module Uxid

class Configuration

attr_accessor :hashid_charset, :hashid_salt, :hashid_size, :nanoid_charset, :nanoid_size, :ulid_charset, :ulid_size
attr_accessor :hashid_charset, :hashid_size, :hashid_salt,
:nanoid_charset, :nanoid_size,
:ulid_charset, :ulid_size,
:uuid_version

def initialize
@hashid_charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Expand All @@ -15,6 +18,7 @@ def initialize
@nanoid_size = 21
@ulid_charset = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
@ulid_size = 26
@uuid_version = 4
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/lite/uxid/hashid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Lite
module Uxid
class Hashid < Reversible
class Hashid < Base::Reversible

def encode
encode_chars((id + coder_salt) << coder_size)
Expand Down
71 changes: 0 additions & 71 deletions lib/lite/uxid/irreversible.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/lite/uxid/nanoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Lite
module Uxid
class Nanoid < Irreversible
class Nanoid < Base::Irreversible

def encode
(0...coder_size).each_with_object(+"") do |i, str|
Expand Down
34 changes: 0 additions & 34 deletions lib/lite/uxid/reversible.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/lite/uxid/ulid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Lite
module Uxid
class Ulid < Irreversible
class Ulid < Base::Irreversible

MASK = 0x1f

Expand Down
9 changes: 7 additions & 2 deletions lib/lite/uxid/uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

module Lite
module Uxid
class Uuid < Irreversible
class Uuid < Base::Irreversible

def encode
SecureRandom.uuid
case coder_version
when 7
SecureRandom.uuid_v7
else
SecureRandom.uuid
end
end

end
Expand Down

0 comments on commit efaebcf

Please sign in to comment.