Sha256: 01184dad33b6f16c93ac8f3e0a8b945f5930521aee5a43176f0daa8f76909c36

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require File.dirname(__FILE__) + '/simplehasher/railtie' if defined?(Rails)

module SimpleHasher

  def self.encode(id)
    length = self.config.allowed_chars.length

    while id > length -1
      hash = self.config.allowed_chars[id % length,1].concat( hash || "" )
      id = (id / length).floor
    end

    self.config.allowed_chars[id,1].concat(hash || "")
  end

  def self.decode(hash)
    length = self.config.allowed_chars.length
    size = hash.length - 1
    array = hash.split('')
    id = self.config.allowed_chars.index(array.pop)
    i = 0
    array.each do |c| 
      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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple-hasher-0.0.7 lib/simplehasher.rb