class RubyDice::Wordlist

Attributes

words[R]

Public Class Methods

new(filename_or_options = nil) click to toggle source

Damn ugly… lets refactor sometime

# 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

Public Instance Methods

random(count = 5) click to toggle source
# 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