Class: PasswordStrengthValidator::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/password_strength_validator/validator.rb

Instance Method Summary collapse

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, options = {})
  parse_and_set_password(password)
  @options  = OpenStruct.new(DEFAULT_OPTIONS.merge(options))
end

Instance Method Details

#enough_length?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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