Class: Passphrase::Language

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

Overview

This class encapsulates the #count and #[] queries against the “languages” table in the “words” SQLite 3 database. It also provides the #only method which allows a subset of languages to be specified.

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Language

Returns a new instance of Language.



8
9
10
11
12
# File 'lib/passphrase/wordlist_database.rb', line 8

def initialize(db)
  @languages = []
  sql = "SELECT language FROM languages"
  db.execute(sql).each { |lang| @languages << lang.first }
end

Instance Method Details

#[](index) ⇒ String

Returns the language corresponding to the given index.

Parameters:

  • index (Integer)

    selects a specific row in the languages table

Returns:

  • (String)

    the language corresponding to the given index



21
22
23
# File 'lib/passphrase/wordlist_database.rb', line 21

def [](index)
  @languages[index]
end

#allArray

Returns all rows in the languages table.

Returns:

  • (Array)

    all rows in the languages table



26
27
28
# File 'lib/passphrase/wordlist_database.rb', line 26

def all
  @languages
end

#countInteger

Returns the number of rows in the languages table.

Returns:

  • (Integer)

    the number of rows in the languages table



15
16
17
# File 'lib/passphrase/wordlist_database.rb', line 15

def count
  @languages.size
end

#only(language_list) ⇒ self

Returns to allow chaining methods.

Parameters:

  • language_list (Array)

    restrict languages to those in the list

Returns:

  • (self)

    to allow chaining methods



32
33
34
35
36
37
38
39
# File 'lib/passphrase/wordlist_database.rb', line 32

def only(language_list)
  return self if /^all$/ =~ language_list.first
  validate(language_list)
  @languages.keep_if do |language|
    language_list.any? { |l| language.match("^#{l}") }
  end
  self
end