simple-hasher 0.0.4 → 0.0.5
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.
- data/lib/simplehasher/version.rb +1 -1
- data/lib/simplehasher.rb +29 -7
- data/spec/simple-hasher_spec.rb +1 -1
- metadata +2 -2
data/lib/simplehasher/version.rb
CHANGED
data/lib/simplehasher.rb
CHANGED
@@ -1,27 +1,49 @@
|
|
1
1
|
class SimpleHasher
|
2
|
-
ALLOWED_CHARS = "CLK0oXklU2d6RvTrS1aDBx9GfN3e7FnQOtsmPi85MYq4AWHbZIuwJEgjVhpzyc"
|
3
2
|
|
4
3
|
def self.encode(id)
|
5
|
-
length =
|
4
|
+
length = self.config.allowed_chars.length
|
6
5
|
|
7
6
|
while id > length -1
|
8
|
-
hash =
|
7
|
+
hash = self.config.allowed_chars[id % length,1].concat( hash || "" )
|
9
8
|
id = (id / length).floor
|
10
9
|
end
|
11
10
|
|
12
|
-
|
11
|
+
self.config.allowed_chars[id,1].concat(hash || "")
|
13
12
|
end
|
14
13
|
|
15
14
|
def self.decode(hash)
|
16
|
-
length =
|
15
|
+
length = self.config.allowed_chars.length
|
17
16
|
size = hash.length - 1
|
18
17
|
array = hash.split('')
|
19
|
-
id =
|
18
|
+
id = self.config.allowed_chars.index(array.pop)
|
20
19
|
i = 0
|
21
20
|
array.each do |c|
|
22
|
-
id +=
|
21
|
+
id += self.config.allowed_chars.index(c) * (length ** (size - i))
|
23
22
|
i += 1
|
24
23
|
end
|
25
24
|
id
|
26
25
|
end
|
26
|
+
|
27
|
+
def self.allowed_chars
|
28
|
+
self.config.allowed_chars
|
29
|
+
end
|
30
|
+
|
31
|
+
# Handles the configuration values for the module
|
32
|
+
class Config
|
33
|
+
attr_accessor :allowed_chars
|
34
|
+
|
35
|
+
def initialize
|
36
|
+
@allowed_chars = "CLK0oXklU2d6RvTrS1aDBx9GfN3e7FnQOtsmPi85MYq4AWHbZIuwJEgjVhpzyc"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns of initializes the Module configuration
|
41
|
+
def self.config
|
42
|
+
@@config ||= Config.new
|
43
|
+
end
|
44
|
+
|
45
|
+
# Allows for setting config values via a block
|
46
|
+
def self.configure
|
47
|
+
yield self.config
|
48
|
+
end
|
27
49
|
end
|
data/spec/simple-hasher_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe SimpleHasher do
|
|
5
5
|
describe "encode" do
|
6
6
|
it 'should encode a single digit integer' do
|
7
7
|
num = 1
|
8
|
-
SimpleHasher.encode(num).should== SimpleHasher
|
8
|
+
SimpleHasher.encode(num).should== SimpleHasher.allowed_chars[num]
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'should encode an integer into a multi-character string' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-hasher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-05-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|