Class: XKPassword::Generator
- Inherits:
-
Object
- Object
- XKPassword::Generator
- Defined in:
- lib/xkpassword/generator.rb
Constant Summary collapse
- DEFAULTS =
{ max_length: 8, min_length: 4, separator: '-', words: 4, }
Instance Attribute Summary collapse
-
#words ⇒ Object
readonly
Returns the value of attribute words.
Instance Method Summary collapse
-
#generate(options = nil) ⇒ Object
options = { separator: ‘ ’, words: 4, min_length: 4, max_length: 8 }.
-
#initialize ⇒ Generator
constructor
A new instance of Generator.
Constructor Details
#initialize ⇒ Generator
Returns a new instance of Generator.
13 14 15 |
# File 'lib/xkpassword/generator.rb', line 13 def initialize @words = XKPassword::Words.new end |
Instance Attribute Details
#words ⇒ Object (readonly)
Returns the value of attribute words.
11 12 13 |
# File 'lib/xkpassword/generator.rb', line 11 def words @words end |
Instance Method Details
#generate(options = nil) ⇒ Object
options =
separator: ' ',
words: 4,
min_length: 4,
max_length: 8
generator = XKPassword::Generator.new generator.generate(options)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/xkpassword/generator.rb', line 26 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 |