Class: XKPassword::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/xkpassword/generator.rb

Constant Summary collapse

DEFAULTS =
{
  max_length: 8,
  min_length: 4,
  separator: '-',
  words: 4,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGenerator

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

#wordsObject (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(options = nil)
  options ||= {}
  options = DEFAULTS.merge(options)
  length_vals = (options[:min_length]..options[:max_length]).to_a

  data = options[:words].times.map do
    word = words.random(length_vals.sample)
    upcase = [true, false].sample
    word = word.upcase if upcase
    word
  end
  
  data.join(options[:separator])
end