-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
136 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
module Lite | ||
module Uxid | ||
class Ulid < Irreversible | ||
class Ulid < Base::Irreversible | ||
|
||
MASK = 0x1f | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters