Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions lib/ulid.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# frozen_string_literal: true

require 'time'
require 'securerandom'
require "time"
require "securerandom"

require 'ulid/version'
require 'ulid/constants'
require 'ulid/identifier'
require 'ulid/generate'
require 'ulid/parse'
require 'ulid/compare'
require "ulid/version"
require "ulid/constants"
require "ulid/identifier"
require "ulid/generate"
require "ulid/parse"
require "ulid/compare"

module ULID
include Constants
Expand Down Expand Up @@ -81,6 +82,4 @@ def self.min_ulid_at(at_time)
def self.max_ulid_at(at_time)
Identifier.new(at_time, MAX_ENTROPY).ulid
end

end

20 changes: 11 additions & 9 deletions lib/ulid/compare.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
# frozen_string_literal: true

module ULID
module Compare
def >(other)
case other
when self.class
self.ulid > other.ulid
ulid > other.ulid
when Time
self.time > other
time > other
when String
self.ulid > other
ulid > other
end
end

def <(other)
case other
when self.class
self.ulid < other.ulid
ulid < other.ulid
when Time
self.time < other
time < other
when String
self.ulid < other
ulid < other
end
end

def <=>(other)
case other
when self.class
self.ulid <=> other.ulid
ulid <=> other.ulid
when Time
self.time <=> other
time <=> other
when String
self.ulid <=> other
ulid <=> other
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/ulid/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
module ULID
module Constants
# smallest representable time
MIN_TIME = ([0] * 6).pack('C' * 6)
MIN_TIME = ([0] * 6).pack("C" * 6)
# largest representable time
MAX_TIME = ([255] * 6).pack('C' * 6)
MAX_TIME = ([255] * 6).pack("C" * 6)

# smallest possible seed value
MIN_ENTROPY = ([0] * 10).pack('C' * 10)
MIN_ENTROPY = ([0] * 10).pack("C" * 10)
# largest possible seed value
MAX_ENTROPY = ([255] * 10).pack('C' * 10)
MAX_ENTROPY = ([255] * 10).pack("C" * 10)

# Crockford's Base32 (https://www.crockford.com/base32.html) Differs from Base32 in the following ways:
# * Excludes I, L, O and U
Expand Down
8 changes: 4 additions & 4 deletions lib/ulid/generate.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'securerandom'
require 'ulid/constants'
require "securerandom"
require "ulid/constants"

module ULID
module Generate
Expand All @@ -24,11 +24,11 @@ def encode32

# returns the binary uint128 in base16 UUID format
def encode16
self.bytes.unpack("H8H4H4H4H*").join("-")
bytes.unpack("H8H4H4H4H*").join("-")
end

def encode10
(hi, lo) = self.bytes.unpack('Q>Q>')
(hi, lo) = bytes.unpack("Q>Q>")
(hi << 64) | lo
end

Expand Down
23 changes: 13 additions & 10 deletions lib/ulid/identifier.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'ulid/parse'
require 'ulid/generate'
require 'ulid/compare'
# frozen_string_literal: true

require "ulid/parse"
require "ulid/generate"
require "ulid/compare"

module ULID
class Identifier
Expand All @@ -25,13 +27,14 @@ def initialize(start = nil, seed = nil)
@time = start.time
@seed = start.seed
when NilClass, Time
@time = (start || Time.now).utc
if seed == nil
@time = start || Time.now
if seed.nil?
@seed = random_bytes
else
if seed.size != 10 || seed.encoding != Encoding::ASCII_8BIT
raise ArgumentError.new("seed error, seed value must be 10 bytes encoded as Encoding::ASCII_8BIT")
raise ArgumentError, "seed error, seed value must be 10 bytes encoded as Encoding::ASCII_8BIT"
end

@seed = seed
end
when String
Expand All @@ -47,24 +50,24 @@ def initialize(start = nil, seed = nil)
# parse UUID string into bytes
@bytes = decode16(start)
else
raise ArgumentError.new("invalid ULID or UUID string - must be 16, 26, or 36 characters")
raise ArgumentError, "invalid ULID or UUID string - must be 16, 26, or 36 characters"
end

raise ArgumentError.new("invalid ULID or UUID") if @bytes.size != 16
raise ArgumentError, "invalid ULID or UUID" if @bytes.size != 16

@time, @seed = unpack_decoded_bytes(@bytes)
when Integer
# parse integer (BigNum) into bytes
@bytes = decode10(start)

raise ArgumentError.new("invalid ULID or UUID") if @bytes.size != 16
raise ArgumentError, "invalid ULID or UUID" if @bytes.size != 16

@time, @seed = unpack_decoded_bytes(@bytes)
when Array
# parse Array(16) into bytes
@bytes = start.pack("C*")

raise ArgumentError.new("invalid Byte Array") if @bytes.size != 16
raise ArgumentError, "invalid Byte Array" if @bytes.size != 16

@time, @seed = unpack_ulid_bytes(@bytes)
else
Expand Down
Loading