Class: Translatomatic::Database
- Inherits:
-
Object
- Object
- Translatomatic::Database
- Extended by:
- DefineOptions
- Includes:
- Util
- Defined in:
- lib/translatomatic/database.rb
Class Attribute Summary collapse
-
.options ⇒ Object
readonly
Returns the value of attribute options.
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.
-
#migrate ⇒ void
Run outstanding migrations against the database.
Methods included from Util
Constructor Details
#initialize(options = {}) ⇒ Database
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/translatomatic/database.rb', line 21 def initialize( = {}) @env = [:database_env] || DEFAULT_ENV @db_config = database_config(@env, ) @env_config = @db_config raise "no environment '#{@env}' in #{db_config_path}" unless @env_config[@env] @env_config = @env_config[@env] || {} ActiveRecord::Base.configurations = @db_config ActiveRecord::Tasks::DatabaseTasks.env = @env ActiveRecord::Tasks::DatabaseTasks.db_dir = DB_PATH ActiveRecord::Tasks::DatabaseTasks.root = DB_PATH ActiveRecord::Tasks::DatabaseTasks.database_configuration = @db_config create unless exists? migrate end |
Class Attribute Details
.options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/translatomatic/database.rb', line 8 def end |
Class Method Details
.enabled?(options = {}) ⇒ Boolean
12 13 14 |
# File 'lib/translatomatic/database.rb', line 12 def enabled?( = {}) new().connect end |
Instance Method Details
#connect ⇒ boolean
Connect to the database
39 40 41 42 43 44 45 46 |
# File 'lib/translatomatic/database.rb', line 39 def connect begin ActiveRecord::Base.establish_connection(@env_config) true rescue LoadError false end end |
#create ⇒ boolean
Create the database
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/translatomatic/database.rb', line 78 def create begin ActiveRecord::Tasks::DatabaseTasks.create(@env_config) log.debug "Database created." true rescue LoadError => e log.debug "Database could not be created: " + e. false end end |
#disconnect ⇒ void
This method returns an undefined value.
Disconnect from the database
50 51 52 |
# File 'lib/translatomatic/database.rb', line 50 def disconnect ActiveRecord::Base.remove_connection end |
#drop ⇒ void
This method returns an undefined value.
Drop the database
91 92 93 94 95 |
# File 'lib/translatomatic/database.rb', line 91 def drop disconnect ActiveRecord::Tasks::DatabaseTasks.drop(@env_config) log.debug "Database deleted." end |
#exists? ⇒ Boolean
Test if the database exists
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/translatomatic/database.rb', line 56 def exists? begin return true if sqlite_database_exists? return false unless connect ActiveRecord::Base.connection.tables rescue return false end true end |
#migrate ⇒ void
This method returns an undefined value.
Run outstanding migrations against the database
69 70 71 72 73 74 |
# File 'lib/translatomatic/database.rb', line 69 def migrate return false unless connect ActiveRecord::Migrator.migrate(MIGRATIONS_PATH) ActiveRecord::Base.clear_cache! log.debug "Database migrated." end |