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.
Constant Summary collapse
- DEFAULTS =
{ max_length: 8, min_length: 4, separator: '-', words: 4, }
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.
17 18 19 |
# File 'lib/xkpassword/generator.rb', line 17 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
7 8 9 |
# File 'lib/xkpassword/generator.rb', line 7 def words @words end |
Instance Method Details
#generate(options = nil) ⇒ String
Generates a password absed on the configuration provided.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/xkpassword/generator.rb', line 41 def generate( = nil) ||= {} = DEFAULTS.merge() length_vals = ([:min_length]..[:max_length]).to_a data = [:words].times.map do word = words.random(length_vals.sample) upcase = [true, false].sample word = word.upcase if upcase word end data.join([:separator]) end |