Class: UXID::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/uxid/encoder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Encoder

Returns a new instance of Encoder.



7
8
9
# File 'lib/uxid/encoder.rb', line 7

def initialize model
  @model = model
end

Class Method Details

.encode(model) ⇒ Object



3
4
5
# File 'lib/uxid/encoder.rb', line 3

def self.encode model
  new(model).encode
end

Instance Method Details

#encodeObject



11
12
13
14
15
16
# File 'lib/uxid/encoder.rb', line 11

def encode
  @model.time_encoded = encode_time
  @model.entropy_encoded = public_send "encode_entropy_#{@model.rand_size}"

  @model.encoded
end

#encode_entropy_0Object



18
# File 'lib/uxid/encoder.rb', line 18

def encode_entropy_0; ""; end

#encode_entropy_1Object



20
21
22
23
24
25
# File 'lib/uxid/encoder.rb', line 20

def encode_entropy_1
  b0, _b1 = @model.entropy_bytes

  UXID::CROCKFORD_ENCODING[(b0&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b0&7)<<2)]
end

#encode_entropy_10Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/uxid/encoder.rb', line 141

def encode_entropy_10
  b0, b1, b2, b3, b4, b5, b6, b7, b8, b9 = @model.entropy_bytes

  UXID::CROCKFORD_ENCODING[(b0&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b0&7)<<2)  | ((b1&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b1&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b1&1)<<4)  | ((b2&240)>>4)] +
  UXID::CROCKFORD_ENCODING[((b2&15)<<1) | ((b3&128)>>7)] +
  UXID::CROCKFORD_ENCODING[(b3&124)>>2] +
  UXID::CROCKFORD_ENCODING[((b3&3)<<3)  | ((b4&224)>>5)] +
  UXID::CROCKFORD_ENCODING[b4&31] +
  UXID::CROCKFORD_ENCODING[(b5&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b5&7)<<2)  | ((b6&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b6&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b6&1)<<4)  | ((b7&240)>>4)] +
  UXID::CROCKFORD_ENCODING[((b7&15)<<1) | ((b8&128)>>7)] +
  UXID::CROCKFORD_ENCODING[(b8&124)>>2] +
  UXID::CROCKFORD_ENCODING[((b8&3)<<3)  | ((b9&224)>>5)] +
  UXID::CROCKFORD_ENCODING[b9&31]
end

#encode_entropy_2Object



27
28
29
30
31
32
33
34
# File 'lib/uxid/encoder.rb', line 27

def encode_entropy_2
  b0, b1 = @model.entropy_bytes

  UXID::CROCKFORD_ENCODING[(b0&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b0&7)<<2)  | ((b1&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b1&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b1&1)<<4)]
end

#encode_entropy_3Object



36
37
38
39
40
41
42
43
44
# File 'lib/uxid/encoder.rb', line 36

def encode_entropy_3
  b0, b1, b2 = @model.entropy_bytes

  UXID::CROCKFORD_ENCODING[(b0&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b0&7)<<2)  | ((b1&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b1&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b1&1)<<4)  | ((b2&240)>>4)] +
  UXID::CROCKFORD_ENCODING[((b2&15)<<1)]
end

#encode_entropy_4Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/uxid/encoder.rb', line 46

def encode_entropy_4
  b0, b1, b2, b3 = @model.entropy_bytes

  UXID::CROCKFORD_ENCODING[(b0&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b0&7)<<2)  | ((b1&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b1&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b1&1)<<4)  | ((b2&240)>>4)] +
  UXID::CROCKFORD_ENCODING[((b2&15)<<1) | ((b3&128)>>7)] +
  UXID::CROCKFORD_ENCODING[(b3&124)>>2] +
  UXID::CROCKFORD_ENCODING[((b3&3)<<3)]
end

#encode_entropy_5Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/uxid/encoder.rb', line 58

def encode_entropy_5
  b0, b1, b2, b3, b4 = @model.entropy_bytes

  UXID::CROCKFORD_ENCODING[(b0&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b0&7)<<2)  | ((b1&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b1&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b1&1)<<4)  | ((b2&240)>>4)] +
  UXID::CROCKFORD_ENCODING[((b2&15)<<1) | ((b3&128)>>7)] +
  UXID::CROCKFORD_ENCODING[(b3&124)>>2] +
  UXID::CROCKFORD_ENCODING[((b3&3)<<3)  | ((b4&224)>>5)] +
  UXID::CROCKFORD_ENCODING[b4&31]
end

#encode_entropy_6Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/uxid/encoder.rb', line 71

def encode_entropy_6
  b0, b1, b2, b3, b4, b5 = @model.entropy_bytes

  UXID::CROCKFORD_ENCODING[(b0&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b0&7)<<2)  | ((b1&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b1&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b1&1)<<4)  | ((b2&240)>>4)] +
  UXID::CROCKFORD_ENCODING[((b2&15)<<1) | ((b3&128)>>7)] +
  UXID::CROCKFORD_ENCODING[(b3&124)>>2] +
  UXID::CROCKFORD_ENCODING[((b3&3)<<3)  | ((b4&224)>>5)] +
  UXID::CROCKFORD_ENCODING[b4&31] +
  UXID::CROCKFORD_ENCODING[(b5&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b5&7)<<2)]
end

#encode_entropy_7Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/uxid/encoder.rb', line 86

def encode_entropy_7
  b0, b1, b2, b3, b4, b5, b6 = @model.entropy_bytes

  UXID::CROCKFORD_ENCODING[(b0&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b0&7)<<2)  | ((b1&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b1&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b1&1)<<4)  | ((b2&240)>>4)] +
  UXID::CROCKFORD_ENCODING[((b2&15)<<1) | ((b3&128)>>7)] +
  UXID::CROCKFORD_ENCODING[(b3&124)>>2] +
  UXID::CROCKFORD_ENCODING[((b3&3)<<3)  | ((b4&224)>>5)] +
  UXID::CROCKFORD_ENCODING[b4&31] +
  UXID::CROCKFORD_ENCODING[(b5&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b5&7)<<2)  | ((b6&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b6&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b6&1)<<4)]
end

#encode_entropy_8Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/uxid/encoder.rb', line 103

def encode_entropy_8
  b0, b1, b2, b3, b4, b5, b6, b7 = @model.entropy_bytes

  UXID::CROCKFORD_ENCODING[(b0&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b0&7)<<2)  | ((b1&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b1&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b1&1)<<4)  | ((b2&240)>>4)] +
  UXID::CROCKFORD_ENCODING[((b2&15)<<1) | ((b3&128)>>7)] +
  UXID::CROCKFORD_ENCODING[(b3&124)>>2] +
  UXID::CROCKFORD_ENCODING[((b3&3)<<3)  | ((b4&224)>>5)] +
  UXID::CROCKFORD_ENCODING[b4&31] +
  UXID::CROCKFORD_ENCODING[(b5&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b5&7)<<2)  | ((b6&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b6&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b6&1)<<4)  | ((b7&240)>>4)] +
  UXID::CROCKFORD_ENCODING[((b7&15)<<1)]
end

#encode_entropy_9Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/uxid/encoder.rb', line 121

def encode_entropy_9
  b0, b1, b2, b3, b4, b5, b6, b7, b8 = @model.entropy_bytes

  UXID::CROCKFORD_ENCODING[(b0&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b0&7)<<2)  | ((b1&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b1&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b1&1)<<4)  | ((b2&240)>>4)] +
  UXID::CROCKFORD_ENCODING[((b2&15)<<1) | ((b3&128)>>7)] +
  UXID::CROCKFORD_ENCODING[(b3&124)>>2] +
  UXID::CROCKFORD_ENCODING[((b3&3)<<3)  | ((b4&224)>>5)] +
  UXID::CROCKFORD_ENCODING[b4&31] +
  UXID::CROCKFORD_ENCODING[(b5&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b5&7)<<2)  | ((b6&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b6&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b6&1)<<4)  | ((b7&240)>>4)] +
  UXID::CROCKFORD_ENCODING[((b7&15)<<1) | ((b8&128)>>7)] +
  UXID::CROCKFORD_ENCODING[(b8&124)>>2] +
  UXID::CROCKFORD_ENCODING[((b8&3)<<3)]
end

#encode_timeObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/uxid/encoder.rb', line 162

def encode_time
  # Get the 5 bytes of the 48bit time
  b0, b1, b2, b3, b4, b5 = @model.time_bytes

  # Split these 5 bytes up into 10 characters using the encoding
  # and join them together as a string
  UXID::CROCKFORD_ENCODING[(b0&224)>>5] +
  UXID::CROCKFORD_ENCODING[b0&31] +
  UXID::CROCKFORD_ENCODING[(b1&248)>>3] +
  UXID::CROCKFORD_ENCODING[((b1&7)<<2)  | ((b2&192)>>6)] +
  UXID::CROCKFORD_ENCODING[(b2&62)>>1] +
  UXID::CROCKFORD_ENCODING[((b2&1)<<4)  | ((b3&240)>>4)] +
  UXID::CROCKFORD_ENCODING[((b3&15)<<1) | ((b4&128)>>7)] +
  UXID::CROCKFORD_ENCODING[(b4&124)>>2] +
  UXID::CROCKFORD_ENCODING[((b4&3)<<3)  | ((b5&224)>>5)] +
  UXID::CROCKFORD_ENCODING[b5&31]
end