ruby_xid 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ruby_xid.rb +48 -56
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43e2108601f1c726d195b4646fad23295f90facd04601df09003fdde47baffbe
4
- data.tar.gz: af678e9fba6abd5d322e87393232084a3b012b64a4c1f0dc0a8a254f4e3410c5
3
+ metadata.gz: 8f11215b569bbc9b0acc216e30731fcd0dc84a3a26d7e771c131820ede1e0f8d
4
+ data.tar.gz: 44dc8ffb01a7a9409699af142df483c30056afa5d981925deb35b9476dbb433c
5
5
  SHA512:
6
- metadata.gz: 9de910d88a77f965b806a39bb4270d12371680f03b57f16b9646ec0582d2394b5933686d718d5e7bfc3ad473af3b059819a3bc6a3ea0aa1d45baaa082637c114
7
- data.tar.gz: c1a87195ce2ea293e23c0459d7e5bdc9e5d3a6d507a8d5a40d3857c227a9036d45fed24d274e0816b36750c2c7315f38fc4cce863a19595df53634e09f29bd9e
6
+ metadata.gz: c1b5be1db1fc6c817d78ba1fff9229c69c7d9d5a5ba83a40c1302f5f23bf8adea0837e53a03eb93faf7505b5d4e581aab43351c73dbefc27fbfd763bd3eb2c67
7
+ data.tar.gz: 6ae7fe4347442396c7ccc7fb6ae8240329f0ef9b9290ca14da7cf899a9befbfa1e96143abf7fb9857f931982b95f3af4e2dc52799392cf46769c66ae1c5afe8f
data/lib/ruby_xid.rb CHANGED
@@ -1,32 +1,31 @@
1
+ # frozen_string_literal: true
1
2
  # Xid implementatin in Ruby
3
+ require 'socket'
4
+ require 'securerandom'
5
+ require 'date'
6
+
2
7
  class Xid
3
- require 'socket'
4
- require 'securerandom'
5
- require 'date'
6
8
 
7
9
  RAW_LEN = 12
8
10
  TRIM_LEN = 20
9
11
 
10
- attr_accessor :value
12
+ @@generator = nil
11
13
 
12
14
  def initialize(id = nil)
13
- @mutex = Mutex.new
14
- init_rand_int
15
- @pid = Process.pid
16
- @machine_id = real_machine_id
17
- unless id.nil?
18
- # Decoded array
19
- @value = id
20
- else
21
- generate_xid
22
- end
15
+ @@generator ||= Generator.new(init_rand_int, real_machine_id)
16
+ @byte_str = id ? id.map(&:chr).join('') : @@generator.next_xid
23
17
  end
24
18
 
25
- def next_xid
26
- @value = generate_xid
19
+ def next
20
+ @string = @value = nil
21
+ @byte_str = @@generator.next_xid
27
22
  string
28
23
  end
29
24
 
25
+ def value
26
+ @value ||= @byte_str.chars.map(&:ord)
27
+ end
28
+
30
29
  def pid
31
30
  # type: () -> int
32
31
  (value[7] << 8 | value[8])
@@ -51,37 +50,49 @@ class Xid
51
50
  value[0] << 24 | value[1] << 16 | value[2] << 8 | value[3]
52
51
  end
53
52
 
53
+ def inspect
54
+ "Xid('#{string}')"
55
+ end
56
+
57
+ def to_s
58
+ string
59
+ end
60
+
54
61
  def string
55
62
  # type: () -> str
56
- Base32.b32encode(value)[0..TRIM_LEN - 1]
63
+ @string ||= Base32.b32encode(value)[0..TRIM_LEN - 1]
57
64
  end
58
65
 
59
66
  def bytes
60
67
  # type: () -> str
61
- value.map(&:chr).join('')
68
+ @byte_str
62
69
  end
63
70
 
64
71
  def init_rand_int
65
72
  # type: () -> int
66
- @rand_int = begin
67
- buford = SecureRandom.hex(3).scan(/.{2}/m).map(&:hex)
68
- buford[0] << 16 | buford[1] << 8 | buford[2]
69
- end
73
+ SecureRandom.random_number(16_777_215)
74
+ end
75
+
76
+ def real_machine_id
77
+ # type: () -> int
78
+ Digest::MD5.digest(Socket.gethostname).unpack('N')[0]
79
+ rescue
80
+ init_rand_int
70
81
  end
71
82
 
72
83
  def ==(other_xid)
73
84
  # type: (Xid) -> bool
74
- string < other_xid.string
85
+ to_s == other_xid.to_s
75
86
  end
76
87
 
77
88
  def <(other_xid)
78
89
  # type: (Xid) -> bool
79
- string < other_xid.string
90
+ to_s < other_xid.to_s
80
91
  end
81
92
 
82
93
  def >(other_xid)
83
94
  # type: (Xid) -> bool
84
- string > other_xid.string
95
+ to_s > other_xid.to_s
85
96
  end
86
97
 
87
98
  def self.from_string(str)
@@ -97,44 +108,25 @@ class Xid
97
108
  Object.const_get(name).new(val)
98
109
  end
99
110
 
100
- private
111
+ # Xid Generator
112
+ class Generator
113
+ attr_accessor :value
101
114
 
102
- def real_machine_id
103
- # type: () -> List[int]
104
- hostname = Socket.gethostname.encode('utf-8')
105
- val = Digest::MD5.digest(hostname)[0..3]
106
- val.chars.map(&:ord)
107
- rescue
108
- SecureRandom.hex(3).scan(/.{2}/m).map(&:hex)
115
+ def initialize(rand_val = nil, machine_id = 0)
116
+ @mutex = Mutex.new
117
+ @rand_int = rand_val || rand(65_535)
118
+ @pid = Process.pid
119
+ @machine_id = machine_id
109
120
  end
110
121
 
111
- def generate_xid
112
- # type: () -> List[int]
113
- now = Time.now.to_i
114
- @value = Array.new(RAW_LEN, 0)
115
-
116
- @value[0] = (now >> 24) & 0xff
117
- @value[1] = (now >> 16) & 0xff
118
- @value[2] = (now >> 8) & 0xff
119
- @value[3] = now & 0xff
120
-
121
- @value[4] = @machine_id[0]
122
- @value[5] = @machine_id[1]
123
- @value[6] = @machine_id[2]
124
-
125
- @value[7] = (@pid >> 8) & 0xff
126
- @value[8] = @pid & 0xff
127
-
122
+ def next_xid
123
+ # () -> str
128
124
  @mutex.synchronize do
129
125
  @rand_int += 1
130
126
  end
131
-
132
- @value[9] = (@rand_int >> 16) & 0xff
133
- @value[10] = (@rand_int >> 8) & 0xff
134
- @value[11] = @rand_int & 0xff
135
-
136
- @value
127
+ [::Time.new.to_i, @machine_id, @pid, @rand_int << 8].pack('N NX n NX')
137
128
  end
129
+ end
138
130
  end
139
131
 
140
132
  require_relative 'xid/base32'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_xid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Valarpiraichandran
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-12 00:00:00.000000000 Z
11
+ date: 2019-12-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: a.valarpirai@gmail.com
@@ -30,7 +30,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 1.9.3
34
34
  required_rubygems_version: !ruby/object:Gem::Requirement
35
35
  requirements:
36
36
  - - ">="