Module: Gentok::ClassMethods

Defined in:
lib/gentok.rb

Instance Method Summary collapse

Instance Method Details

#gentok(*args, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gentok.rb', line 9

def gentok *args, &block
  opts = {
    :length => 8,
    :chars  => ('a'..'z').to_a + ('A'..'Z').to_a
  }
  opts.merge! args.pop if args.last.kind_of? Hash
  args.each do |value|
    before_create do |record|
      if block
        record.validate_unique value, &block
      else
        record.validate_unqiue value do
          Array.new(opts[:length]) { opts[:chars].to_a[rand(options[:chars].to_a.size)] }.join
        end
      end
    end # end before create block
  end # end args block
end