Module: UidHelper

Extended by:
UidHelper
Included in:
UidHelper
Defined in:
lib/rails_com/utils/uid_helper.rb

Instance Method Summary collapse

Instance Method Details

#decode_uuid(uuid, prefix: true) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rails_com/utils/uid_helper.rb', line 33

def decode_uuid(uuid, prefix: true)
  if prefix
    str_arr = uuid.split('-')
    str = str_arr[1..-1].join
  else
    str_arr = uuid.split('-')
    str = str_arr.join
  end

  if str.size >= 12
    str = str[0..11]
    str.to_i(36).to_s
  elsif str.size >= 6 && str.size < 12
    str = str[0..5]
    str.to_i(36).to_s
  else
    raise 'Can not parse the format string!'
  end
end

#nsec_uuid(prefix = '', suffix = rand_string) ⇒ Object



16
17
18
19
20
# File 'lib/rails_com/utils/uid_helper.rb', line 16

def nsec_uuid(prefix = '', suffix = rand_string)
  time = Time.now
  str = time.to_i.to_s + time.nsec.to_s
  uuid str.to_i, prefix, suffix
end

#rand_string(len = 4) ⇒ Object



53
54
55
# File 'lib/rails_com/utils/uid_helper.rb', line 53

def rand_string(len = 4)
  len.times.map { ((0..9).to_a + ('A'..'Z').to_a).sample }.join
end

#sec_uuid(prefix = '', suffix = rand_string(2)) ⇒ Object



28
29
30
31
# File 'lib/rails_com/utils/uid_helper.rb', line 28

def sec_uuid(prefix = '', suffix = rand_string(2))
  time = Time.now
  uuid time.to_i, prefix, suffix
end

#usec_uuid(prefix = '', suffix = rand_string(2)) ⇒ Object



22
23
24
25
26
# File 'lib/rails_com/utils/uid_helper.rb', line 22

def usec_uuid(prefix = '', suffix = rand_string(2))
  time = Time.now
  str = time.to_i.to_s + time.usec.to_s
  uuid str.to_i, prefix, suffix
end

#uuid(int, prefix = '', suffix = '') ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/rails_com/utils/uid_helper.rb', line 4

def uuid(int, prefix = '', suffix = '')
  str = int.to_s(36)
  str = str + suffix
  str = str.upcase.scan(/.{1,4}/).join('-')

  if prefix.present?
    str = prefix + '-' + str
  end

  str
end