Class: RubyDice::Wordlist

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename_or_options = nil) ⇒ Wordlist

Damn ugly… lets refactor sometime



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby-dice/wordlist.rb', line 8

def initialize(filename_or_options = nil)
  options = {}
  filename = nil

  if filename_or_options.is_a?(Hash)
    options = filename_or_options
    filename = options[:wordlist]
  else
    filename = filename_or_options
  end

  filename ||= 'diceware.wordlist.asc'
  filename += '.txt' if File.extname(filename) == ''

  search_paths = [
                   File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'assets')),
                   File.join(Dir.home, '.wordlists')
                 ]

  # Too convoluted?
  unless full_path = (File.exists?(filename) ? filename : nil)
    path = search_paths.detect { |path| File.exists? File.join(path, filename) }
    full_path = File.join(path, filename)
  end

  File.open(full_path, 'r:UTF-8') { |f| @words = f.read }
  @words = @words.match(/(11111.+66666.+?)\n/m)[1].split("\n").map { |l| l.split(/[\t ]/)[1].strip }
end

Instance Attribute Details

#wordsObject (readonly)

Returns the value of attribute words.



5
6
7
# File 'lib/ruby-dice/wordlist.rb', line 5

def words
  @words
end

Instance Method Details

#random(count = 5) ⇒ Object



37
38
39
40
41
42
# File 'lib/ruby-dice/wordlist.rb', line 37

def random(count = 5)
  (1..count).inject([]) do |selected|
    random = SecureRandom.random_number(@words.size)
    selected << @words[random]
  end
end