Skip to content

Commit

Permalink
Rework generators
Browse files Browse the repository at this point in the history
  • Loading branch information
drexed committed Jul 31, 2019
1 parent e0ada22 commit 689eb9d
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Metrics/BlockLength:
Exclude:
- 'spec/**/**/*'
- '*.gemspec'
RSpec/MultipleExpectations:
Enabled: false
Style/Documentation:
Enabled: false
Style/ExpandPathArguments:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Added benchmarks
### Changed
- Changed instance method names to match class names
- Changed nanoid encoder to be faster
- Changed ulid encoder to be faster

Expand Down
16 changes: 8 additions & 8 deletions lib/lite/uxid/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ class << self

def encode(id)
klass = new(id)
klass.encode_uxid
klass.encode
end

def decode(id)
klass = new(id)
klass.decode_uxid
klass.decode
end

end

def encode_uxid
uxid_encode_chars((@id + encoding_salt) << encoding_length)
def encode
encode_chars((@id + encoding_salt) << encoding_length)
end

def decode_uxid
(uxid_decode_chars(@id) >> encoding_length) - encoding_salt
def decode
(decode_chars(@id) >> encoding_length) - encoding_salt
end

private

def uxid_encode_chars(id)
def encode_chars(id)
return '0' if id.zero?
return nil if id.negative?

Expand All @@ -47,7 +47,7 @@ def uxid_encode_chars(id)
str
end

def uxid_decode_chars(id)
def decode_chars(id)
pos = 0
num = 0
len = id.length
Expand Down
17 changes: 10 additions & 7 deletions lib/lite/uxid/nanoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ class << self

def encode
klass = new
klass.nanoid_encode
klass.encode
end

end

# rubocop:disable Performance/UnfreezeString
def nanoid_encode
chars = encoding_chars.split('')
def encode
str = ''
len = encoding_length
chars = encoding_chars.split(str)

encoding_length.times.reduce(''.dup) do |str, _|
str << chars.sample
while len.positive?
str += chars.sample
len -= 1
end

str
end
# rubocop:enable Performance/UnfreezeString

end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/lite/uxid/record/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ module Hash

included do
after_create :callback_generate_uxid!, if: proc { respond_to?(:uxid) }
end

def self.find_by_uxid(uxid)
class_methods do
def find_by_uxid(uxid)
decoded_id = Lite::Uxid::Hash.decode(uxid)
find_by(id: decoded_id)
end

def self.find_by_uxid!(uxid)
def find_by_uxid!(uxid)
record = find_by_uxid(uxid)
return record unless record.nil?

Expand Down
6 changes: 4 additions & 2 deletions lib/lite/uxid/record/nanoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ module Nanoid

included do
before_create :callback_generate_uxid!, if: proc { respond_to?(:uxid) }
end

def self.find_by_uxid(uxid)
class_methods do
def find_by_uxid(uxid)
find_by(uxid: uxid)
end

def self.find_by_uxid!(uxid)
def find_by_uxid!(uxid)
record = find_by_uxid(uxid)
return record unless record.nil?

Expand Down
6 changes: 4 additions & 2 deletions lib/lite/uxid/record/ulid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ module Ulid

included do
before_create :callback_generate_uxid!, if: proc { respond_to?(:uxid) }
end

def self.find_by_uxid(uxid)
class_methods do
def find_by_uxid(uxid)
find_by(uxid: uxid)
end

def self.find_by_uxid!(uxid)
def find_by_uxid!(uxid)
record = find_by_uxid(uxid)
return record unless record.nil?

Expand Down
20 changes: 10 additions & 10 deletions lib/lite/uxid/ulid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class << self

def encode
klass = new
klass.uxid_encode
klass.encode
end

end

def uxid_encode
oct = uxid_octect
def encode
oct = octect
ele = '0' * encoding_length
pos = encoding_length - 1

Expand All @@ -33,22 +33,22 @@ def uxid_encode

private

def uxid_bytes
"#{uxid_unixtime_48bit}#{SecureRandom.random_bytes(10)}"
def bytes
"#{unixtime_48bit}#{SecureRandom.random_bytes(10)}"
end

def uxid_octect
(hi, lo) = uxid_bytes.unpack('Q>Q>')
def octect
(hi, lo) = bytes.unpack('Q>Q>')
(hi << 64) | lo
end

def uxid_unixtime_flex
def unixtime_ms
time = Time.respond_to?(:current) ? Time.current : Time.now
(time.to_f * 1_000).to_i
end

def uxid_unixtime_48bit
[uxid_unixtime_flex].pack('Q>')[2..-1]
def unixtime_48bit
[unixtime_ms].pack('Q>')[2..-1]
end

end
Expand Down
24 changes: 8 additions & 16 deletions spec/lite/uxid/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,22 @@
require 'spec_helper'

RSpec.describe Lite::Uxid::Hash do
let(:encoder) { described_class.new(10) }
let(:decoder) { described_class.new('q5D8inm0') }
let(:nid) { 10 }
let(:hid) { 'q5D8inm0' }
let(:encoder) { described_class.new(nid) }
let(:decoder) { described_class.new(hid) }

describe '.decode' do
it 'to be 10' do
expect(described_class.decode('q5D8inm0')).to eq(10)
end
end

describe '.decode_uxid' do
it 'to be 10' do
expect(decoder.decode_uxid).to eq(10)
expect(described_class.decode(hid)).to eq(nid)
expect(decoder.decode).to eq(nid)
end
end

describe '.encode' do
it 'to be "q5D8inm0"' do
expect(described_class.encode(10)).to eq('q5D8inm0')
end
end

describe '.encode_uxid' do
it 'to be "q5D8inm0"' do
expect(encoder.encode_uxid).to eq('q5D8inm0')
expect(described_class.encode(nid)).to eq(hid)
expect(encoder.encode).to eq(hid)
end
end

Expand Down
9 changes: 3 additions & 6 deletions spec/lite/uxid/nanoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@

describe '.encode' do
it 'to be 26' do
expect(described_class.encode.length).to eq(26)
end
end
n1 = 26

describe '.nanoid_encode' do
it 'to be 26' do
expect(encoder.nanoid_encode.length).to eq(26)
expect(described_class.encode.length).to eq(n1)
expect(encoder.encode.length).to eq(n1)
end
end

Expand Down
9 changes: 3 additions & 6 deletions spec/lite/uxid/ulid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@

describe '.encode' do
it 'to be 26' do
expect(described_class.encode.length).to eq(26)
end
end
n1 = 26

describe '.uxid_encode' do
it 'to be 26' do
expect(encoder.uxid_encode.length).to eq(26)
expect(described_class.encode.length).to eq(n1)
expect(encoder.encode.length).to eq(n1)
end
end

Expand Down

0 comments on commit 689eb9d

Please sign in to comment.