Class: PasswordStrengthValidator::Validator
- Inherits:
-
Object
- Object
- PasswordStrengthValidator::Validator
- Defined in:
- lib/password_strength_validator/validator.rb
Instance Method Summary collapse
- #enough_length? ⇒ Boolean
- #has_enough_digits? ⇒ Boolean
- #has_enough_symbols? ⇒ Boolean
- #has_lowercase? ⇒ Boolean
- #has_uppercase? ⇒ Boolean
-
#initialize(password, options = {}) ⇒ Validator
constructor
A new instance of Validator.
- #parse_and_set_password(password) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(password, options = {}) ⇒ Validator
Returns a new instance of Validator.
16 17 18 19 |
# File 'lib/password_strength_validator/validator.rb', line 16 def initialize(password, = {}) parse_and_set_password(password) @options = OpenStruct.new(DEFAULT_OPTIONS.merge()) end |
Instance Method Details
#enough_length? ⇒ Boolean
25 26 27 |
# File 'lib/password_strength_validator/validator.rb', line 25 def enough_length? @password.length.between?(@options.min_length, @options.max_length) end |
#has_enough_digits? ⇒ Boolean
37 38 39 |
# File 'lib/password_strength_validator/validator.rb', line 37 def has_enough_digits? @password.each_char.grep(/[0-9]/).size >= @options.number_of_digits end |
#has_enough_symbols? ⇒ Boolean
41 42 43 44 |
# File 'lib/password_strength_validator/validator.rb', line 41 def has_enough_symbols? return false if @password.empty? @password.each_char.map(&:ord).select { |c| SYMBOLS.include?(c) }.size >= @options.number_of_symbols end |
#has_lowercase? ⇒ Boolean
33 34 35 |
# File 'lib/password_strength_validator/validator.rb', line 33 def has_lowercase? !@options.require_lowercase || !!@password.match(/[a-z]/) end |
#has_uppercase? ⇒ Boolean
29 30 31 |
# File 'lib/password_strength_validator/validator.rb', line 29 def has_uppercase? !@options.require_uppercase || !!@password.match(/[A-Z]/) end |
#parse_and_set_password(password) ⇒ Object
46 47 48 |
# File 'lib/password_strength_validator/validator.rb', line 46 def parse_and_set_password(password) @password = password || '' end |
#valid? ⇒ Boolean
21 22 23 |
# File 'lib/password_strength_validator/validator.rb', line 21 def valid? enough_length? && has_uppercase? && has_lowercase? && has_enough_digits? && has_enough_symbols? end |