Class: Poefy::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/poefy/sqlite3.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.desc(database_name) ⇒ Object

Get the description of a database.



38
39
40
41
42
43
44
45
# File 'lib/poefy/sqlite3.rb', line 38

def self.desc database_name
  begin
    sql = "SELECT comment FROM comment;"
    Database::single_exec!(database_name, sql).flatten.first
  rescue
    ''
  end
end

.listObject

List all database files in the directory. Does not include databases used for testing.



29
30
31
32
33
34
35
# File 'lib/poefy/sqlite3.rb', line 29

def self.list
  Dir[Poefy.root + '/data/*.db'].map do |i|
    File.basename(i, '.db')
  end.reject do |i|
    i.start_with?('spec_')
  end.sort - ['test']
end

.list_with_descObject

List all database files and their descriptions.



48
49
50
51
52
53
54
55
56
# File 'lib/poefy/sqlite3.rb', line 48

def self.list_with_desc
  Database::list.map do |i|
    begin
      [i, Database::desc(i)]
    rescue
      [i, '']
    end
  end.to_h
end

.path(database_name) ⇒ Object

Get the path of a database.



59
60
61
# File 'lib/poefy/sqlite3.rb', line 59

def self.path database_name
  Poefy.root + '/data/' + File.basename(database_name, '.db') + '.db'
end

.single_exec!(database_name, sql) ⇒ Object

Open a connection, execute a query, close the connection.



19
20
21
22
23
24
25
# File 'lib/poefy/sqlite3.rb', line 19

def self.single_exec! database_name, sql
  path = Database::path database_name
  con = SQLite3::Database.open path
  rs = con.execute sql
  con.close
  rs
end

Instance Method Details

#countObject

The number of lines in the table.



81
82
83
84
85
# File 'lib/poefy/sqlite3.rb', line 81

def count
  return 0 unless exist?
  sql = "SELECT COUNT(*) AS num FROM #{table};"
  execute!(sql).first['num'].to_i
end

#descObject

Get/set the description of the database.



72
73
74
# File 'lib/poefy/sqlite3.rb', line 72

def desc
  Database::desc @name
end

#desc=(description) ⇒ Object



75
76
77
78
# File 'lib/poefy/sqlite3.rb', line 75

def desc=(description)
  execute! "DELETE FROM comment;"
  execute! "INSERT INTO comment VALUES ( ? );", description.to_s
end

#exist?Boolean

See if the database file exists or not.

Returns:

  • (Boolean)


88
89
90
# File 'lib/poefy/sqlite3.rb', line 88

def exist?
  File.exist?(db_file)
end

#rhymes(word, key = nil) ⇒ Object

Get all rhyming lines for the word.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/poefy/sqlite3.rb', line 93

def rhymes word, key = nil
  return nil if word.nil?

  sql = <<-SQL
    SELECT rhyme, final_word, syllables, line
    FROM lines
    WHERE rhyme = ?
    ORDER BY rhyme, final_word, syllables, line
  SQL
  output = word.to_phrase.rhymes.keys.map do |rhyme|
    rs = execute!(sql, [rhyme]).to_a
    rs.each{ |a| a.reject!{ |k| k.is_a? Numeric }}
  end.flatten

  if !key.nil? and %w[rhyme final_word syllables line].include?(key)
    output.map!{ |i| i[key] }
  end
  output
end

#typeObject

This is the type of database that is being used. It is also used as a signifier that a database has been specified.



67
68
69
# File 'lib/poefy/sqlite3.rb', line 67

def type
  'sqlite3'
end