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



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



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

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

#allArray



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

def all
  @languages
end

#countInteger



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

def count
  @languages.size
end

#only(language_list) ⇒ self



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