class Strgen
Usage:
length = 10 exclude = %w(!)
end
Constants
- ALPHA
- ALPHANUM
- LOWERCASE
- NUMBERS
- SYMBOLS
- TYPES
- UPPERCASE
- VERSION
Public Class Methods
generate(&block)
click to toggle source
# File lib/strgen.rb, line 25 def self.generate(&block) proxy = new if block_given? block.arity > 0 ? block.call(proxy) : proxy.instance_eval(&block) end proxy.merge_defaults! raise InvalidLengthError if proxy.length < 1 previous = nil current = nil acc = '' characters = proxy.character_set # store the last char in case we want to # avoid repeating characters proxy.length.times do loop do current = characters.sample break if !proxy.exclude?(current) && (!proxy.repeat || current != previous) end acc += current end acc end
Public Instance Methods
alpha?()
click to toggle source
# File lib/strgen.rb, line 84 def alpha? nil if alphanum end
character_set()
click to toggle source
# File lib/strgen.rb, line 67 def character_set # TODO: can we do this better? a = [] a.concat(SYMBOLS) if symbols a.concat(ALPHANUM) if alphanum a.concat(ALPHA) if alpha? || alpha a.concat(NUMBERS) if alpha? || numbers a.concat(LOWERCASE) if letters? || lowercase a.concat(UPPERCASE) if letters? || uppercase a end
exclude?(val)
click to toggle source
# File lib/strgen.rb, line 79 def exclude?(val) return false if exclude.nil? exclude.include? val end
letters?()
click to toggle source
# File lib/strgen.rb, line 88 def letters? nil if alpha end
merge_defaults!()
click to toggle source
# File lib/strgen.rb, line 55 def merge_defaults! TYPES.each_with_object({}) do |type, acc| acc[type] = true acc end.tap do |hash| hash.merge! length: 12, repeat: true end.each do |method, default| next unless send(method.to_sym).nil? send("#{method}=".to_sym, default) end end
pick_all?()
click to toggle source
# File lib/strgen.rb, line 92 def pick_all? !lowercase && !uppercase && !alpha && !alphanum && !symbols end