Class: Zxcvbn::TesterBuilder
- Inherits:
-
Object
- Object
- Zxcvbn::TesterBuilder
- Defined in:
- lib/zxcvbn/tester_builder.rb
Overview
Fluent builder for constructing a Tester with custom word lists and options.
Obtain a builder via tester_builder and call #build to get the Tester.
Example:
tester = Zxcvbn
.tester_builder
.add_word_list('company', %w[acme corp])
.max_password_length(75)
.build
Constant Summary collapse
- DEFAULT_MAX_PASSWORD_LENGTH =
Default maximum password length used when neither #max_password_length nor the
ZXCVBN_MAX_PASSWORD_LENGTHenvironment variable is set. 256
Instance Method Summary collapse
- #add_word_list(name, words) ⇒ self
- #build ⇒ Tester
-
#initialize ⇒ TesterBuilder
constructor
A new instance of TesterBuilder.
- #max_password_length(length) ⇒ self
Constructor Details
#initialize ⇒ TesterBuilder
Returns a new instance of TesterBuilder.
23 24 25 26 |
# File 'lib/zxcvbn/tester_builder.rb', line 23 def initialize @word_lists = {} @max_password_length = nil end |
Instance Method Details
#add_word_list(name, words) ⇒ self
32 33 34 35 36 37 38 39 40 |
# File 'lib/zxcvbn/tester_builder.rb', line 32 def add_word_list(name, words) if Data::RESERVED_NAMES.include?(name) raise ArgumentError, "#{name.inspect} is a reserved dictionary name; use a different name for custom word lists" end @word_lists[name] = Array(words) self end |
#build ⇒ Tester
56 57 58 59 60 |
# File 'lib/zxcvbn/tester_builder.rb', line 56 def build data = Data.new @word_lists.each_pair { |name, words| data.add_word_list(name, words) } Tester.new(data: data.freeze, max_password_length: effective_max_password_length).freeze end |
#max_password_length(length) ⇒ self
45 46 47 48 49 50 51 52 |
# File 'lib/zxcvbn/tester_builder.rb', line 45 def max_password_length(length) unless length.is_a?(Integer) && length.positive? raise ArgumentError, "max_password_length must be a positive integer; got #{length.inspect}" end @max_password_length = length self end |