Class: Util::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/record-locator-util.rb

Class Method Summary collapse

Class Method Details

.decode(string) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/record-locator-util.rb', line 27

def self.decode(string)
  string = string.to_s
  return string if string.split('').include?('1') || string.split('').include?('0') # as 0 and 1 are included into exceptional chars
  ring = Util::DECODER[Util::BASE27]
  base = Util::BASE27.length
  string.reverse.chars.enum_for(:each_with_index).inject(0) do |sum,(char,i)|
    sum + ring[char] * base**i
  end
end

.encode(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/record-locator-util.rb', line 16

def self.encode(value)
  ring = Util::ENCODER[Util::BASE27]
  base = Util::BASE27.length
  result = []
  until value == 0
    result << ring[ value % base ]
    value /= base
  end
  result.reverse.join
end