Class: Xid

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil) ⇒ Xid

Returns a new instance of Xid.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ruby_xid.rb', line 12

def initialize(id = nil)
  @mutex = Mutex.new
  init_rand_int
  @pid = Process.pid
  @machine_id = real_machine_id
  unless id.nil?
    # Decoded array
    @value = id
  else
    generate_xid
  end
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



10
11
12
# File 'lib/ruby_xid.rb', line 10

def value
  @value
end

Class Method Details

.from_string(str) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruby_xid.rb', line 87

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



77
78
79
80
# File 'lib/ruby_xid.rb', line 77

def <(other_xid)
  # type: (Xid) -> bool
  string < other_xid.string
end

#==(other_xid) ⇒ Object



72
73
74
75
# File 'lib/ruby_xid.rb', line 72

def ==(other_xid)
  # type: (Xid) -> bool
  string < other_xid.string
end

#>(other_xid) ⇒ Object



82
83
84
85
# File 'lib/ruby_xid.rb', line 82

def >(other_xid)
  # type: (Xid) -> bool
  string > other_xid.string
end

#bytesObject



59
60
61
62
# File 'lib/ruby_xid.rb', line 59

def bytes
  # type: () -> str
  value.map(&:chr).join('')
end

#counterObject



35
36
37
38
# File 'lib/ruby_xid.rb', line 35

def counter
  # type: () -> int
  value[9] << 16 | value[10] << 8 | value[11]
end

#datetimeObject



45
46
47
# File 'lib/ruby_xid.rb', line 45

def datetime
  Time.at(time).to_datetime
end

#init_rand_intObject



64
65
66
67
68
69
70
# File 'lib/ruby_xid.rb', line 64

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

#machineObject



40
41
42
43
# File 'lib/ruby_xid.rb', line 40

def machine
  # type: () -> str
  value[4..6].map(&:chr).join('')
end

#next_xidObject



25
26
27
28
# File 'lib/ruby_xid.rb', line 25

def next_xid
  @value = generate_xid
  string
end

#pidObject



30
31
32
33
# File 'lib/ruby_xid.rb', line 30

def pid
  # type: () -> int
  (value[7] << 8 | value[8])
end

#stringObject



54
55
56
57
# File 'lib/ruby_xid.rb', line 54

def string
  # type: () -> str
  Base32.b32encode(value)[0..TRIM_LEN - 1]
end

#timeObject



49
50
51
52
# File 'lib/ruby_xid.rb', line 49

def time
  # type: () -> int
  value[0] << 24 | value[1] << 16 | value[2] << 8 | value[3]
end