lib/mongoid/token/generator.rb in mongoid_token-2.1.1 vs lib/mongoid/token/generator.rb in mongoid_token-2.1.2

- old
+ new

@@ -10,11 +10,11 @@ # Any pattern can be followed by a number, representing how many of that type to generate module Mongoid module Token module Generator - REPLACE_PATTERN = /%((?<character>[cCdDpsw]{1})(?<length>\d+(,\d+)?)?)/ + REPLACE_PATTERN = /%((?<character>[cCdDhHpsw]{1})(?<length>\d+(,\d+)?)?)/ def self.generate(pattern) pattern.gsub REPLACE_PATTERN do |match| match_data = $~ type = match_data[:character] @@ -27,10 +27,14 @@ up_character(length) when 'd' digits(length) when 'D' integer(length) + when 'h' + digits(length, 16) + when 'H' + integer(length, 16) when 's' alphanumeric(length) when 'w' alpha(length) when 'p' @@ -50,15 +54,15 @@ def self.up_character(length = 1) self.rand_string_from_chars ('A'..'Z').to_a, length end - def self.integer(length = 1) - (rand(10**length - 10**(length-1)) + 10**(length-1)).to_s + def self.integer(length = 1, base = 10) + (rand(base**length - base**(length-1)) + base**(length-1)).to_s(base) end - def self.digits(length = 1) - rand(10**length).to_s.rjust(length, "0") + def self.digits(length = 1, base = 10) + rand(base**length).to_s(base).rjust(length, "0") end def self.alpha(length = 1) self.rand_string_from_chars (('A'..'Z').to_a + ('a'..'z').to_a), length end