class Translatomatic::CLI::Database
Database
functions for the command line interface
Public Instance Methods
delete(text_id)
click to toggle source
Delete a text and its translations from the database @param text_id [Number] id of text to delete @return [void]
# File lib/translatomatic/cli/database.rb, line 51 def delete(text_id) run do db = Translatomatic::Database.new(options) text = db.text.find(text_id) raise t('cli.database.text_not_found', id: text_id) unless text text.destroy end end
drop()
click to toggle source
Drop the database
# File lib/translatomatic/cli/database.rb, line 64 def drop run { Translatomatic::Database.new(options).drop } end
info()
click to toggle source
Show information about the database
# File lib/translatomatic/cli/database.rb, line 72 def info run do db = Translatomatic::Database.new(options) 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)
click to toggle source
Search database for the given string @param string [String] String to search for @param locale [String] Optional locale, by default search all locales @return [void]
# File lib/translatomatic/cli/database.rb, line 12 def search(string, locale = nil) run do db = Translatomatic::Database.new(options) # 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
Private Instance Methods
highlight(text, highlighted)
click to toggle source
# File lib/translatomatic/cli/database.rb, line 86 def highlight(text, highlighted) text.gsub(highlighted) { |i| rainbow.wrap(i).bright.inverse } end