Module: UidHelper
Instance Method Summary collapse
- #decode_uuid(uuid, prefix: true) ⇒ Object
- #nsec_uuid(prefix = '', suffix: rand_string, separator: '-') ⇒ Object
- #rand_string(len = 4) ⇒ Object
- #sec_uuid(prefix = '', suffix: rand_string(2), separator: '-') ⇒ Object
- #usec_uuid(prefix = '', suffix: rand_string(2), separator: '-') ⇒ Object
- #uuid(int, prefix: '', suffix: '', separator: '') ⇒ Object
Instance Method Details
#decode_uuid(uuid, prefix: true) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rails_com/utils/uid_helper.rb', line 40 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, separator: '-') ⇒ Object
18 19 20 21 22 |
# File 'lib/rails_com/utils/uid_helper.rb', line 18 def nsec_uuid(prefix = '', suffix: rand_string, separator: '-') time = Time.current str = time.to_i.to_s + time.nsec.to_s uuid str.to_i, prefix: prefix, suffix: suffix, separator: separator end |
#rand_string(len = 4) ⇒ Object
35 36 37 38 |
# File 'lib/rails_com/utils/uid_helper.rb', line 35 def rand_string(len = 4) list = (0..9).to_a + ('A'..'Z').to_a len.times.map { list.sample }.join end |
#sec_uuid(prefix = '', suffix: rand_string(2), separator: '-') ⇒ Object
30 31 32 33 |
# File 'lib/rails_com/utils/uid_helper.rb', line 30 def sec_uuid(prefix = '', suffix: rand_string(2), separator: '-') time = Time.current uuid time.to_i, prefix: prefix, suffix: suffix, separator: separator end |
#usec_uuid(prefix = '', suffix: rand_string(2), separator: '-') ⇒ Object
24 25 26 27 28 |
# File 'lib/rails_com/utils/uid_helper.rb', line 24 def usec_uuid(prefix = '', suffix: rand_string(2), separator: '-') time = Time.current str = time.to_i.to_s + time.usec.to_s uuid str.to_i, prefix: prefix, suffix: suffix, separator: separator end |
#uuid(int, prefix: '', suffix: '', separator: '') ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/rails_com/utils/uid_helper.rb', line 6 def uuid(int, prefix: '', suffix: '', separator: '') str = int.to_s(36) str = str + suffix str = str.upcase.scan(/.{1,4}/).join(separator) if prefix.present? str = prefix + '-' + str end str end |