Class: XKPassword::Generator
- Inherits:
-
Object
- Object
- XKPassword::Generator
- Defined in:
- lib/xkpassword/generator.rb
Overview
The Generator class which finds words based on the requirement and using the provided options to build a new random passowrd. Presets provide convenient defaults for common password styles, and callers can override any preset-derived value by passing explicit generation options.
Constant Summary collapse
- DEFAULT_PRESET =
The preset used when ‘:preset` is omitted.
:xkcd- PRESETS =
Built-in password presets.
-
‘:xkcd` generates 4 words between 4 and 8 letters separated by `-`, preserving the gem’s original default behavior.
-
‘:web32` generates 4 shorter words between 4 and 5 letters separated by `-`.
-
‘:wifi` generates 6 words between 4 and 8 letters separated by `-`.
-
‘:security` generates 6 lowercase words between 4 and 8 letters separated by spaces.
-
‘:apple_id` generates 3 words between 4 and 7 letters separated by `-`.
-
{ xkcd: { case_transform: nil, max_length: 8, min_length: 4, separator: '-', words: 4, }, web32: { case_transform: nil, max_length: 5, min_length: 4, separator: '-', words: 4, }, wifi: { case_transform: nil, max_length: 8, min_length: 4, separator: '-', words: 6, }, security: { case_transform: :downcase, max_length: 8, min_length: 4, separator: ' ', words: 6, }, apple_id: { case_transform: nil, max_length: 7, min_length: 4, separator: '-', words: 3, }, }
- VALID_CASE_TRANSFORMS =
[:upcase, :downcase, :capitalize]
Instance Attribute Summary collapse
-
#words ⇒ XKPassword::Words
readonly
A word database that gen provide you words for the length required.
Instance Method Summary collapse
-
#generate(options = nil) ⇒ String
Generates a password absed on the configuration provided.
-
#initialize ⇒ Generator
constructor
A new instance of Generator.
Constructor Details
#initialize ⇒ Generator
Returns a new instance of Generator.
62 63 64 |
# File 'lib/xkpassword/generator.rb', line 62 def initialize @words = XKPassword::Words.new end |
Instance Attribute Details
#words ⇒ XKPassword::Words (readonly)
A word database that gen provide you words for the length required
9 10 11 |
# File 'lib/xkpassword/generator.rb', line 9 def words @words end |
Instance Method Details
#generate(options = nil) ⇒ String
Generates a password absed on the configuration provided. A preset supplies a base configuration and any explicit options passed in ‘options` override those preset defaults.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/xkpassword/generator.rb', line 110 def generate( = nil) = ( || {}).dup preset = normalize_preset(.delete(:preset)) = PRESETS.fetch(preset).merge() case_transform = normalize_case_transform([:case_transform]) length_vals = ([:min_length]..[:max_length]).to_a data = [:words].times.map do word = words.random(length_vals.sample) transform_word(word, case_transform) end data.join([:separator]) end |