Class: Xid
- Inherits:
-
Object
- Object
- Xid
- Defined in:
- lib/ruby_xid.rb
Overview
Xid implementatin in Ruby
Defined Under Namespace
Classes: Base32
Constant Summary collapse
- RAW_LEN =
12- TRIM_LEN =
20
Class Method Summary collapse
Instance Method Summary collapse
- #<(other_xid) ⇒ Object
- #==(other_xid) ⇒ Object
- #>(other_xid) ⇒ Object
- #bytes ⇒ Object
- #counter ⇒ Object
- #datetime ⇒ Object
- #init_rand_int ⇒ Object
-
#initialize(id = nil) ⇒ Xid
constructor
A new instance of Xid.
- #machine ⇒ Object
- #machine_id ⇒ Object
- #next_xid ⇒ Object
- #pid ⇒ Object
- #string ⇒ Object
- #time ⇒ Object
Constructor Details
#initialize(id = nil) ⇒ Xid
Returns a new instance of Xid.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ruby_xid.rb', line 10 def initialize(id = nil) @mutex = Mutex.new init_rand_int @pid = Process.pid unless id.nil? # Decoded array @value = id else @value = generate_xid end end |
Class Method Details
.from_string(str) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ruby_xid.rb', line 89 def self.from_string(str) val = Base32.b32decode(str) value_check = val.select { |x| x >= 0 && x < 255 } (value_check.length..RAW_LEN - 1).each do |i| value_check[i] = false end raise 'Invalid Xid' unless value_check.all? Object.const_get(name).new(val) end |
Instance Method Details
#<(other_xid) ⇒ Object
79 80 81 82 |
# File 'lib/ruby_xid.rb', line 79 def <(other_xid) # type: (Xid) -> bool string < other_xid.string end |
#==(other_xid) ⇒ Object
74 75 76 77 |
# File 'lib/ruby_xid.rb', line 74 def ==(other_xid) # type: (Xid) -> bool string < other_xid.string end |
#>(other_xid) ⇒ Object
84 85 86 87 |
# File 'lib/ruby_xid.rb', line 84 def >(other_xid) # type: (Xid) -> bool string > other_xid.string end |
#bytes ⇒ Object
61 62 63 64 |
# File 'lib/ruby_xid.rb', line 61 def bytes # type: () -> str @value.map(&:chr).join('') end |
#counter ⇒ Object
32 33 34 35 |
# File 'lib/ruby_xid.rb', line 32 def counter # type: () -> int @value[9] << 16 | @value[10] << 8 | @value[11] end |
#datetime ⇒ Object
42 43 44 |
# File 'lib/ruby_xid.rb', line 42 def datetime Time.at(time).to_datetime end |
#init_rand_int ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/ruby_xid.rb', line 66 def init_rand_int # type: () -> int @rand_int = begin buford = SecureRandom.hex(3).scan(/.{2}/m).map(&:hex) buford[0] << 16 | buford[1] << 8 | buford[2] end end |
#machine ⇒ Object
37 38 39 40 |
# File 'lib/ruby_xid.rb', line 37 def machine # type: () -> str @value[4..7].map(&:chr) end |
#machine_id ⇒ Object
51 52 53 |
# File 'lib/ruby_xid.rb', line 51 def machine_id @machine_id ||= real_machine_id end |
#next_xid ⇒ Object
22 23 24 25 |
# File 'lib/ruby_xid.rb', line 22 def next_xid @value = generate_xid string end |
#pid ⇒ Object
27 28 29 30 |
# File 'lib/ruby_xid.rb', line 27 def pid # type: () -> int (@value[7] << 8 | @value[8]) end |
#string ⇒ Object
55 56 57 58 59 |
# File 'lib/ruby_xid.rb', line 55 def string # type: () -> str byte_value = bytes Base32.b32encode(byte_value).downcase[0..TRIM_LEN - 1] end |
#time ⇒ Object
46 47 48 49 |
# File 'lib/ruby_xid.rb', line 46 def time # type: () -> int @value[0] << 24 | @value[1] << 16 | @value[2] << 8 | @value[3] end |