encode-id 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/encode_id.rb +73 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8aafbd04e7a6c133b708dd0789de4fb17a7134bd702f6d708978039cb66ffab0
|
4
|
+
data.tar.gz: ca6bac5123f111358f50abd1505a041a030461723dd6963d1a604bad3747e401
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e04582c894f1559d8c3e4821f4193d0ae0374d4ba5610d042578edc8e85b4e8f8f063ec19c629171e142c9fa9d852dd9cd4b91455e8fa06dd94fb337dc24cb57
|
7
|
+
data.tar.gz: 6479efa99203658b12d3dc7fea3e4fa6ce269a524688fcac400837dd940bc53ec9098033e6e395cff4e3c5f825fa13f61f39c1c3d515aaadfede8e424a591a02
|
data/lib/encode_id.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
class EncodeId
|
2
|
+
|
3
|
+
attr_reader :max_digits
|
4
|
+
attr_reader :grows
|
5
|
+
|
6
|
+
def initialize(max_digits=6, grows=false)
|
7
|
+
@base=[
|
8
|
+
'VaRCScev0fNjk9HDGlmTobPq7rIsYtux5yzAgBE4pFihUJnK3LMd1Q6XZ',
|
9
|
+
'JK3LMNtuvx5yzABCDE4FGHIP1QRSTUV6XYZabcde0fghijk9lmnopq7rs',
|
10
|
+
'STUV6XYZabcde0fghijk9lmK3LMNnopq7rstuvP1QRx5yzABCDE4FGHIJ',
|
11
|
+
'P1QRk9lmnopq7rsSTdzABCDEuJK3LMN4FGHe0fghijtvx5yUV6XYZabcI',
|
12
|
+
'lmnopDE4FGHe0fghijtuJK3LMNvx5q7rsSTdzABCIPyUV6XYZabc1QRk9',
|
13
|
+
'vx5yzABCDE4FGHUV6XYZk9lmnopq7rsP1QRSTde0fghijtuabcIJK3LMN',
|
14
|
+
'TdzABCIPyUV6XYZabc1QRk9lmnopDE4FGHe0fghijtuJK3LMNvx5q7rsS',
|
15
|
+
'QRSTde0fghijtuabcIJK3LMNvx5yzABCDE4FGHUV6XYZk9lmnopq7rsP1',
|
16
|
+
'4FGHe0fghijtP1QRk9lmnopq7rsSTdzABCDEuJK3LMNvx5yUV6XYZabcI',
|
17
|
+
'q7rsSTdzABCIPyUV6XYZabc1QRk9lmnopDE4FGHe0fghijtuJK3LMNvx5',
|
18
|
+
]
|
19
|
+
@max_digits = max_digits
|
20
|
+
@grows = grows
|
21
|
+
@base_len = @base.length()
|
22
|
+
@n_base = @base[0].length()
|
23
|
+
end
|
24
|
+
|
25
|
+
def encode(id)
|
26
|
+
if(!@grows && id > max = self.max_id())
|
27
|
+
raise "Limit id(#{id}) for #{@max_digits} digits(#{max}) in EncodeId.encode, possible lost of information"
|
28
|
+
end
|
29
|
+
ret = ''
|
30
|
+
aux = id
|
31
|
+
bit = 0
|
32
|
+
while (@grows && aux) || (@max_digits > bit) do
|
33
|
+
a = aux % @n_base
|
34
|
+
ret.concat(@base[bit % @base_len][a])
|
35
|
+
aux = (aux - a) / @n_base
|
36
|
+
bit += 1
|
37
|
+
end
|
38
|
+
return ret
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def decode(c)
|
43
|
+
id = 0
|
44
|
+
x = 1
|
45
|
+
for bit in 0..c.length() - 1
|
46
|
+
v = @base[bit % @base_len].index(c[bit])
|
47
|
+
id += v * x
|
48
|
+
x *= @n_base
|
49
|
+
end
|
50
|
+
return id
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
def max_alias
|
55
|
+
a = ''
|
56
|
+
for bit in 0..@max_digits
|
57
|
+
a.concat(@base[bit % @base_len][-1])
|
58
|
+
end
|
59
|
+
return a
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
def max_id()
|
64
|
+
@n_base.pow(@max_digits)
|
65
|
+
end
|
66
|
+
|
67
|
+
protected
|
68
|
+
|
69
|
+
attr_accessor :base
|
70
|
+
attr_accessor :n_base
|
71
|
+
attr_accessor :base_len
|
72
|
+
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: encode-id
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joseluis Laso
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2012-02-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A useful class to generate a key or hash from a numerical ID
|
14
|
+
email: jlaso@joseluislaso.es
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/encode_id.rb
|
20
|
+
homepage: https://rubygems.org/gems/encode_id
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubygems_version: 3.4.0.dev
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: A useful class to generate a key or hash from a numerical ID
|
43
|
+
test_files: []
|