Class: RubyDice::Passphrase

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-dice/passphrase.rb

Class Method Summary collapse

Class Method Details

.generate(options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby-dice/passphrase.rb', line 3

def self.generate(options = {})
  default_options = { words: 5 }
  options = default_options.merge(options)

  wordlist_options = {}
  wordlist_options[:wordlist] = options.delete(:wordlist)

  wordlist = Wordlist.new(wordlist_options)
  words = wordlist.random(options[:words])

  if !words.detect { |w| w.match(/\d/) } && options[:numbers]
    words[-1] += SecureRandom.random_number(100).to_s
  end

  if options[:camelcase]
    words.map!(&:capitalize)
    words[0] = words[0].downcase
    phrase = words.join
  else
    phrase = words.join(' ')
  end

  phrase
end