class RandomPassword::Base

Constants

DIGITS
LETTERS
SYMBOLS

Public Class Methods

new(**options) click to toggle source
# File lib/random_password/base.rb, line 9
def initialize(**options)
  update(options)
end

Public Instance Methods

generate() click to toggle source
# File lib/random_password/base.rb, line 13
def generate
  password_letters.shuffle(random: Random.new).join[0, @length]
end
update(**options) click to toggle source
# File lib/random_password/base.rb, line 17
def update(**options)
  @length  = options[:length].to_i
  @digits  = options[:digits].to_i
  @symbols = options[:symbols].to_i
  self
end

Private Instance Methods

password_letters() click to toggle source
# File lib/random_password/base.rb, line 26
def password_letters
  passwords = []
  passwords.concat(@digits.times.map { random_digit })
  passwords.concat(@symbols.times.map { random_symbol })
  passwords.concat((@length - @digits - @symbols).times.map { random_letter })
end
random_digit() click to toggle source
# File lib/random_password/base.rb, line 37
def random_digit
  DIGITS.sample
end
random_letter() click to toggle source
# File lib/random_password/base.rb, line 33
def random_letter
  LETTERS.sample
end
random_symbol() click to toggle source
# File lib/random_password/base.rb, line 41
def random_symbol
  SYMBOLS.sample
end