Permalink
Browse files

Move the random key generator to the class instead of the instance

  • Loading branch information...
1 parent 1cb2651 commit 2b3fa7b055fa2298bcef7c391bb7c74f68c2e1bb @jhubert committed Apr 29, 2012
Showing with 10 additions and 8 deletions.
  1. +9 −1 lib/acts_as_keyed/class_methods.rb
  2. +1 −7 lib/acts_as_keyed/instance_methods.rb
@@ -29,8 +29,16 @@ def find(*args)
def key_exists?(k)
exists?(["#{options[:column]} = ?", k])
end
+
+ def generate_key(size = nil)
+ size ||= options[:size]
+
+ code_array=[]
+ 1.upto(options[:size]) { code_array << options[:chars][rand(options[:chars].length)] }
+ code_array.join('')
+ end
end
-
+
include InstanceMethods
end
end
@@ -23,18 +23,12 @@ def key
def create_key
k = nil
100.times do
- k = random_key
+ k = self.class.generate_key
break if !self.class.key_exists?(k)
k = nil
end
raise NoAvailableKeysError if k.nil?
self.key = k
end
-
- def random_key
- code_array=[]
- 1.upto(options[:size]) { code_array << options[:chars][rand(options[:chars].length)] }
- code_array.join('')
- end
end
end

0 comments on commit 2b3fa7b

Please sign in to comment.