class Senha::Base::Generator

A class for handling the generation of a password based on options

Attributes

available_chars[RW]

Public Class Methods

new(options) click to toggle source

Creates a new instance of the Generator class

@return [Generator]

# File lib/senha/base/generator.rb, line 37
def initialize(options)
        @available_chars = Array.new

        @numbers = ('0'..'9').to_a
        @lower_case = ('a'..'z').to_a
        @upper_case = ('A'..'Z').to_a
        @punctuation = %w(. , ! :).to_a
        @symbols = %w(~ ! @ # $ % ^ & * ( ) _).to_a

        if options[:all]
                @available_chars.concat @numbers
                @available_chars.concat @lower_case
                @available_chars.concat @upper_case
                @available_chars.concat @symbols
                @available_chars.concat @punctuation
        else
                if options[:numbers]
                        @available_chars.concat @numbers
                end

                if options[:lowercase]
                        @available_chars.concat @lower_case
                end

                if options[:uppercase]
                        @available_chars.concat @upper_case
                end

                if options[:symbols]
                        @available_chars.concat @symbols
                end

                if options[:punct]
                        @available_chars.concat @punctuation
                end
        end
end

Public Instance Methods

password(length = 10) click to toggle source

Generates a password

@return [String] of the randomly generated password

# File lib/senha/base/generator.rb, line 78
def password(length = 10)
        1.upto(length).collect do |a|
                @available_chars[rand(@available_chars.size)]
        end.join
end