lib/simplehasher.rb in simple-hasher-0.0.4 vs lib/simplehasher.rb in simple-hasher-0.0.5
- old
+ new
@@ -1,27 +1,49 @@
class SimpleHasher
- ALLOWED_CHARS = "CLK0oXklU2d6RvTrS1aDBx9GfN3e7FnQOtsmPi85MYq4AWHbZIuwJEgjVhpzyc"
def self.encode(id)
- length = ALLOWED_CHARS.length
+ length = self.config.allowed_chars.length
while id > length -1
- hash = ALLOWED_CHARS[id % length,1].concat( hash || "" )
+ hash = self.config.allowed_chars[id % length,1].concat( hash || "" )
id = (id / length).floor
end
- ALLOWED_CHARS[id,1].concat(hash || "")
+ self.config.allowed_chars[id,1].concat(hash || "")
end
def self.decode(hash)
- length = ALLOWED_CHARS.length
+ length = self.config.allowed_chars.length
size = hash.length - 1
array = hash.split('')
- id = ALLOWED_CHARS.index(array.pop)
+ id = self.config.allowed_chars.index(array.pop)
i = 0
array.each do |c|
- id += ALLOWED_CHARS.index(c) * (length ** (size - i))
+ id += self.config.allowed_chars.index(c) * (length ** (size - i))
i += 1
end
id
+ end
+
+ def self.allowed_chars
+ self.config.allowed_chars
+ end
+
+ # Handles the configuration values for the module
+ class Config
+ attr_accessor :allowed_chars
+
+ def initialize
+ @allowed_chars = "CLK0oXklU2d6RvTrS1aDBx9GfN3e7FnQOtsmPi85MYq4AWHbZIuwJEgjVhpzyc"
+ end
+ end
+
+ # Returns of initializes the Module configuration
+ def self.config
+ @@config ||= Config.new
+ end
+
+ # Allows for setting config values via a block
+ def self.configure
+ yield self.config
end
end