Class: RwdStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/random_word_generator/rwd_storage.rb

Instance Method Summary collapse

Constructor Details

#initializeRwdStorage

Returns a new instance of RwdStorage.



9
10
11
12
# File 'lib/random_word_generator/rwd_storage.rb', line 9

def initialize
  connect
  check_and_initialize
end

Instance Method Details

#drop_tableObject



27
28
29
# File 'lib/random_word_generator/rwd_storage.rb', line 27

def drop_table
  @db.execute "DROP TABLE IF EXISTS wordlist;"
end

#find_random_wordObject



18
19
20
# File 'lib/random_word_generator/rwd_storage.rb', line 18

def find_random_word
  @db.execute("SELECT word FROM wordlist ORDER BY random() LIMIT 1;").first.first
end

#find_word_of_length(length) ⇒ Object



22
23
24
25
# File 'lib/random_word_generator/rwd_storage.rb', line 22

def find_word_of_length(length)
  row = @db.execute("SELECT word FROM wordlist WHERE length = #{length} ORDER BY random() LIMIT 1;")
  row.first ? row.first.first : nil
end

#setup_databaseObject



31
32
33
34
35
36
# File 'lib/random_word_generator/rwd_storage.rb', line 31

def setup_database
  connect
  drop_table
  create_schema
  load_words
end

#show_tablesObject



14
15
16
# File 'lib/random_word_generator/rwd_storage.rb', line 14

def show_tables
  @db.execute("SELECT name FROM SQLITE_MASTER where type='table'").flatten
end