Class: Translatomatic::Database

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/translatomatic/database.rb

Overview

Database functions

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  @env = options[:database_env] || DEFAULT_ENV
  @db_config = database_config(@env, options)
  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.

Parameters:

  • options (Hash<Symbol,Object>) (defaults to: {})

    Database options

Returns:

  • (boolean)

    True if we can connect to the database



9
10
11
# File 'lib/translatomatic/database.rb', line 9

def enabled?(options = {})
  new(options).connect
end

Instance Method Details

#connectboolean

Connect to the database

Returns:

  • (boolean)

    True if the connection was established



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

#createboolean

Create the database

Returns:

  • (boolean)

    True if the database was created



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.message
  false
end

#disconnectvoid

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

#dropvoid

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

Returns:

  • (Boolean)

    true 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

#localeClass

Shortcut to locale model class

Returns:

  • (Class)

    Translatomatic::Model::Locale



93
94
95
# File 'lib/translatomatic/database.rb', line 93

def locale
  Translatomatic::Model::Locale
end

#migratevoid

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

#textClass

Shortcut to text model class

Returns:

  • (Class)

    Translatomatic::Model::Text



87
88
89
# File 'lib/translatomatic/database.rb', line 87

def text
  Translatomatic::Model::Text
end