short_id 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/short_id.rb +12 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 446d6270267405153659e8d5fa3227d78243d258
|
4
|
+
data.tar.gz: a967dd6ec895cde28380d8f2243b94da02bf6a6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b30de6dca9f3045def645a84c0cd36384289b183955788bda7163a0cf5bcb5d866d1edf1e87d26a7bc93b8922b244e394c1fb21730ddcdbae88326e4d85fd63
|
7
|
+
data.tar.gz: a6578f163d923f52178162107331e9dabe87f5ca98ef511b21371bba99c7723be8fcb352d659e7861a534cd5b238bec58ce3363f73add5fcc73efc1d7d0cc0a8
|
data/lib/short_id.rb
CHANGED
@@ -2,17 +2,19 @@ require_relative 'id_chars.rb'
|
|
2
2
|
|
3
3
|
ID_CHARS_LENGTH = ID_CHARS.length
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
class ShortId
|
6
|
+
def self.int_to_short_id(n)
|
7
|
+
raise "N must be greater than 0" unless n > 0
|
8
|
+
id = ''
|
8
9
|
|
9
|
-
|
10
|
+
places = Math.log(n, ID_CHARS_LENGTH).floor
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
places.downto(0).each do |place|
|
13
|
+
div, remainder = n.divmod(ID_CHARS_LENGTH ** place)
|
14
|
+
id += ID_CHARS[div - 1]
|
15
|
+
n = remainder
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
+
return id
|
19
|
+
end
|
18
20
|
end
|