Class: Translatomatic::CLI::Database
- Defined in:
- lib/translatomatic/cli/database.rb
Overview
Database functions for the command line interface
Instance Method Summary collapse
-
#delete(text_id) ⇒ void
Delete a text and its translations from the database.
-
#drop ⇒ Object
Drop the database.
-
#info ⇒ Object
Show information about the database.
-
#search(string, locale = nil) ⇒ void
Search database for the given string.
Instance Method Details
#delete(text_id) ⇒ void
This method returns an undefined value.
Delete a text and its translations from the database
51 52 53 54 55 56 57 58 |
# File 'lib/translatomatic/cli/database.rb', line 51 def delete(text_id) run do db = Translatomatic::Database.new() text = db.text.find(text_id) raise t('cli.database.text_not_found', id: text_id) unless text text.destroy end end |
#drop ⇒ Object
Drop the database
64 65 66 |
# File 'lib/translatomatic/cli/database.rb', line 64 def drop run { Translatomatic::Database.new().drop } end |
#info ⇒ Object
Show information about the database
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/translatomatic/cli/database.rb', line 72 def info run do db = Translatomatic::Database.new() puts t('cli.database.text_count', count: db.text.count) texts_by_locale = db.text.group(:locale).count texts_by_locale.each do |locale, count| puts format(' (%<locale>s) %<count>d', locale: locale.to_s, count: count) end end end |
#search(string, locale = nil) ⇒ void
This method returns an undefined value.
Search database for the given string
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/translatomatic/cli/database.rb', line 12 def search(string, locale = nil) run do db = Translatomatic::Database.new() # find all matching texts texts = db.text.where('value LIKE ?', "%#{string}%") if locale db_locale = db.locale.from_tag(locale) texts = texts.where(locale: db_locale) end # get all the associated original texts original_texts = texts.where(from_text_id: nil) from_ids = texts.where('from_text_id IS NOT NULL') .select(:from_text_id).collect(&:from_text_id) original2 = db.text.where('id IN (?)', from_ids) original_texts += original2 original_texts.uniq.each do |text| value = highlight(text.value, string) puts puts format('id:%<id>d (%<locale>s) %<value>s', id: text.id, locale: text.locale, value: value) rows = [] text.translations.each do |t| rows << [' -> ', t.provider, "(#{t.locale})", highlight(t.value, string)] end print_table(rows) end end end |