lib/pwm.rb in pwm-1.1.1 vs lib/pwm.rb in pwm-1.2.0
- old
+ new
@@ -1,9 +1,13 @@
require "pwm/version"
module Pwm
+ # Internal: 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.
# This could be overridden if you don't like the default.
#
# Returns the set of characters as an Array.
def self.characters
@@ -22,13 +26,13 @@
#
# Pwm.password(8)
# # => 'oUX4fmqr'
#
# Returns the generated password as a String if length >= 8,
- # or nil is length < 8.
+ # or raises exception if length < 8.
def self.password(length = 16)
if length < 8
- nil
+ raise Pwm::TooShortException, "Requested length #{length} is too short."
else
password = ''
until (password.match(/[A-Z]/) && password.match(/[a-z]/) && password.match(/[0-9]/))
password = (0..length - 1).inject('') do |pw, n|
pw + characters[rand(characters.length)]