lib/pwm.rb in pwm-1.2.2 vs lib/pwm.rb in pwm-1.2.3

- old
+ new

@@ -1,21 +1,25 @@ require 'pwm/version' +## +# This module is the heart of pwm. module Pwm - # Internal: The exception raised when a requested password length is - # too short. + ## + # The exception raised when a requested password length is too short. class TooShortException < Exception; end - # Internal: The set of characters from which passwords will be assembled. + ## + # The set of characters from which passwords will be assembled. # This could be overridden if you don't like the default. # # Returns the set of characters as an Array. def self.characters - (('A'..'Z').to_a + ('a'..'z').to_a + ('2'..'9').to_a) - %w(I O l) + (('A'..'Z').to_a + ('a'..'z').to_a + ('2'..'9').to_a) - %w(I O l) end - # Public: Generate a password. + ## + # Generate a password. # # length - The length of the password. Minimum is 8. Default is 16. # # Examples # @@ -29,20 +33,21 @@ # or raises exception if length < 8. def self.password(length = 16) fail Pwm::TooShortException, "#{length} is too short." if length < 8 password = '' until acceptable?(password) - password = (0..length - 1).reduce('') do |pw, n| + password = (0..length - 1).reduce('') do |pw, _n| pw + characters[rand(characters.length)] end end password end private - # Private: Checks if a password is acceptable. + ## + # Checks if a password is acceptable. # # password - the password to check # # Examples # @@ -53,10 +58,8 @@ # # => false # # Returns true if password has at least one of each required character # class, or false if not. def self.acceptable?(password) - password.match(/[A-Z]/) && - password.match(/[a-z]/) && - password.match(/[0-9]/) + password.match(/[A-Z]/) && password.match(/[a-z]/) && password.match(/[0-9]/) end end