Class: Passphrase::WordlistDatabase

Inherits:
Object
  • Object
show all
Defined in:
lib/passphrase/wordlist_database.rb

Overview

This class encapsulates the Diceware wordlist database file and dispatches one of two table query objects via the #from method.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWordlistDatabase

Returns a new instance of WordlistDatabase.



80
81
82
83
84
85
# File 'lib/passphrase/wordlist_database.rb', line 80

def initialize
  wordlist_file = "wordlist/words.sqlite3"
  wordlist_path = File.join(File.dirname(__FILE__), wordlist_file)
  raise "Wordlist database file not found" unless File.exist?(wordlist_path)
  @db = SQLite3::Database.new(wordlist_path, readonly: true)
end

Class Method Details

.connectObject

A “connect” method seems more natural than “new” when connecting to a database.



76
77
78
# File 'lib/passphrase/wordlist_database.rb', line 76

def self.connect
  new
end

Instance Method Details

#from(table) ⇒ Language, Word Also known as: []

Parameters:

  • table (Symbol)

    the table name

Returns:

  • (Language)

    if the “languages” table is specified

  • (Word)

    if the “words” table is specified



90
91
92
93
94
95
96
97
98
99
# File 'lib/passphrase/wordlist_database.rb', line 90

def from(table)
  case table
  when :languages
    Language.new(@db)
  when :words
    Word.new(@db)
  else
    raise "Unknown table #{table}"
  end
end