lib/mongoid/token/generator.rb in mongoid_token-2.0.2 vs lib/mongoid/token/generator.rb in mongoid_token-2.1.0
- old
+ new
@@ -38,16 +38,20 @@
end
end
end
private
+ def self.rand_string_from_chars(chars, length = 1)
+ Array.new(length).map{ chars.sample }.join
+ end
+
def self.down_character(length = 1)
- Array.new(length).map{['a'..'z'].map{|r|r.to_a}.flatten[rand(26)]}.join
+ self.rand_string_from_chars ('a'..'z').to_a, length
end
def self.up_character(length = 1)
- Array.new(length).map{['A'..'Z'].map{|r|r.to_a}.flatten[rand(26)]}.join
+ 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
end
@@ -55,18 +59,18 @@
def self.digits(length = 1)
rand(10**length).to_s.rjust(length, "0")
end
def self.alpha(length = 1)
- Array.new(length).map{['A'..'Z','a'..'z'].map{|r|r.to_a}.flatten[rand(52)]}.join
+ self.rand_string_from_chars (('A'..'Z').to_a + ('a'..'z').to_a), length
end
def self.alphanumeric(length = 1)
(1..length).collect { (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }.join
end
def self.punctuation(length = 1)
- Array.new(length).map{['.','-','_','=','+','$'].map{|r|r.to_a}.flatten[rand(52)]}.join
+ self.rand_string_from_chars ['.','-','_','=','+','$'], length
end
end
end
end