Sha256: 6e73ab3032eb1cae3a97578b26ffe5b2f3bd4dc8135709694479f4e8fd5102b3

Contents?: true

Size: 981 Bytes

Versions: 1

Compression:

Stored size: 981 Bytes

Contents

# frozen_string_literal: true

module RandomPassword
  class Base
    LETTERS = [*'a'..'z', *'A'..'Z'].freeze
    DIGITS  = [*'0'..'9'].freeze
    SYMBOLS = %w(! " # $ % & ' ( ) * + , - . / \\ : ; ? @ [ ] ^ _ ` { | } ~).freeze

    def initialize(**options)
      update(options)
    end

    def generate
      password_letters.shuffle(random: Random.new).join[0, @length]
    end

    def update(**options)
      @length  = options[:length].to_i
      @digits  = options[:digits].to_i
      @symbols = options[:symbols].to_i
      self
    end

    private

    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

    def random_letter
      LETTERS.sample
    end

    def random_digit
      DIGITS.sample
    end

    def random_symbol
      SYMBOLS.sample
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
random_password-0.1.1 lib/random_password/base.rb