Class: RwdStorage
- Inherits:
-
Object
- Object
- RwdStorage
- Defined in:
- lib/random_word_generator/rwd_storage.rb
Instance Method Summary collapse
- #drop_table ⇒ Object
- #find_random_word ⇒ Object
- #find_word_of_length(length) ⇒ Object
-
#initialize ⇒ RwdStorage
constructor
A new instance of RwdStorage.
- #setup_database ⇒ Object
- #show_tables ⇒ Object
Constructor Details
#initialize ⇒ RwdStorage
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_table ⇒ Object
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_word ⇒ Object
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_database ⇒ Object
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_tables ⇒ Object
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 |