Class: Translatomatic::Database
- Inherits:
-
Object
- Object
- Translatomatic::Database
- Includes:
- Util
- Defined in:
- lib/translatomatic/database.rb
Overview
Database functions
Class Method Summary collapse
-
.enabled?(options = {}) ⇒ boolean
True if we can connect to the database.
Instance Method Summary collapse
-
#connect ⇒ boolean
Connect to the database.
-
#create ⇒ boolean
Create the database.
-
#disconnect ⇒ void
Disconnect from the database.
-
#drop ⇒ void
Drop the database.
-
#exists? ⇒ Boolean
Test if the database exists.
-
#initialize(options = {}) ⇒ Database
constructor
A new instance of Database.
-
#locale ⇒ Class
Shortcut to locale model class.
-
#migrate ⇒ void
Run outstanding migrations against the database.
-
#text ⇒ Class
Shortcut to text model class.
Constructor Details
#initialize(options = {}) ⇒ Database
Returns a new instance of Database.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/translatomatic/database.rb', line 14 def initialize( = {}) @env = [:database_env] || DEFAULT_ENV @db_config = database_config(@env, ) env_config = @db_config unless env_config[@env] raise t('database.no_environment', env: @env, file: db_config_path) end @env_config = env_config[@env] || {} init_active_record create unless exists? end |
Class Method Details
.enabled?(options = {}) ⇒ boolean
Returns True if we can connect to the database.
9 10 11 |
# File 'lib/translatomatic/database.rb', line 9 def enabled?( = {}) new().connect end |
Instance Method Details
#connect ⇒ boolean
Connect to the database
28 29 30 31 32 33 34 |
# File 'lib/translatomatic/database.rb', line 28 def connect ActiveRecord::Base.establish_connection(@env_config) migrate true rescue LoadError false end |
#create ⇒ boolean
Create the database
67 68 69 70 71 72 73 74 75 |
# File 'lib/translatomatic/database.rb', line 67 def create ActiveRecord::Tasks::DatabaseTasks.create(@env_config) log.debug t('database.created') true rescue LoadError => e log.debug t('database.could_not_create') log.error e. false end |
#disconnect ⇒ void
This method returns an undefined value.
Disconnect from the database
38 39 40 |
# File 'lib/translatomatic/database.rb', line 38 def disconnect ActiveRecord::Base.remove_connection end |
#drop ⇒ void
This method returns an undefined value.
Drop the database
79 80 81 82 83 |
# File 'lib/translatomatic/database.rb', line 79 def drop disconnect ActiveRecord::Tasks::DatabaseTasks.drop(@env_config) log.debug t('database.deleted') end |
#exists? ⇒ Boolean
Test if the database exists
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/translatomatic/database.rb', line 44 def exists? begin return true if sqlite_database_exists? return false unless connect ActiveRecord::Base.connection.tables rescue StandardError return false end true end |
#locale ⇒ Class
Shortcut to locale model class
93 94 95 |
# File 'lib/translatomatic/database.rb', line 93 def locale Translatomatic::Model::Locale end |
#migrate ⇒ void
This method returns an undefined value.
Run outstanding migrations against the database
57 58 59 60 61 62 63 |
# File 'lib/translatomatic/database.rb', line 57 def migrate return if @migrated ActiveRecord::Migrator.migrate(MIGRATIONS_PATH) ActiveRecord::Base.clear_cache! log.debug t('database.migrated') @migrated = true end |
#text ⇒ Class
Shortcut to text model class
87 88 89 |
# File 'lib/translatomatic/database.rb', line 87 def text Translatomatic::Model::Text end |