Class: Lite::Uxid::Hashid

Inherits:
Base
  • Object
show all
Defined in:
lib/lite/uxid/hashid.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#encoding_base

Constructor Details

#initialize(id) ⇒ Hashid

Returns a new instance of Hashid.


7
8
9
10
# File 'lib/lite/uxid/hashid.rb', line 7

def initialize(id)
  @id = id
  super()
end

Class Method Details

.decode(id) ⇒ Object


19
20
21
22
# File 'lib/lite/uxid/hashid.rb', line 19

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

.encode(id) ⇒ Object


14
15
16
17
# File 'lib/lite/uxid/hashid.rb', line 14

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

Instance Method Details

#decodeObject


30
31
32
# File 'lib/lite/uxid/hashid.rb', line 30

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

#encodeObject


26
27
28
# File 'lib/lite/uxid/hashid.rb', line 26

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