Class: Specify::Configuration::DBConfig

Inherits:
Config
  • Object
show all
Defined in:
lib/specify/configuration/db_config.rb

Overview

A class that represents a Specify database configuration.

Instance Attribute Summary collapse

Attributes inherited from Config

#dir_names, #hosts

Instance Method Summary collapse

Methods inherited from Config

#add_database, #add_host, empty, #saved?, #touch

Constructor Details

#initialize(host, database, file = nil) ⇒ DBConfig

-> Configuration::DBConfig Creates a new instance host: String, the name or IP of the MySQL/MariaDB host database: String, the name of the database file: the YAML file (path) containg the configuration


14
15
16
17
18
19
20
21
22
23
# File 'lib/specify/configuration/db_config.rb', line 14

def initialize(host, database, file = nil)
  super(file)
  @host = host
  @database = database
  @port = hosts.dig @host, :port
  @user_name = params&.dig :db_user, :name
  @user_password = params&.dig :db_user, :password
  @session_user = params&.fetch :sp_user, nil
  @saved = known? ? true : false
end

Instance Attribute Details

#databaseObject

Returns the value of attribute database


7
8
9
# File 'lib/specify/configuration/db_config.rb', line 7

def database
  @database
end

#hostObject

Returns the value of attribute host


7
8
9
# File 'lib/specify/configuration/db_config.rb', line 7

def host
  @host
end

#portObject

Returns the value of attribute port


7
8
9
# File 'lib/specify/configuration/db_config.rb', line 7

def port
  @port
end

#session_userObject

Returns the value of attribute session_user


7
8
9
# File 'lib/specify/configuration/db_config.rb', line 7

def session_user
  @session_user
end

#user_nameObject

Returns the value of attribute user_name


7
8
9
# File 'lib/specify/configuration/db_config.rb', line 7

def user_name
  @user_name
end

Instance Method Details

#changed_password?Boolean

-> true or false Returns true if the user_password in memory differs from the :password in the params of the YAML file.

Returns:

  • (Boolean)

123
124
125
# File 'lib/specify/configuration/db_config.rb', line 123

def changed_password?
  params[:db_user][:password] != @user_password
end

#changed_port?Boolean

-> true or false Returns true if the port in memory differs from the :port in the params of the YAML file.

Returns:

  • (Boolean)

130
131
132
# File 'lib/specify/configuration/db_config.rb', line 130

def changed_port?
  hosts[host][:port] != port
end

#changed_session_user?Boolean

-> true or false Returns true if the session_user in memory differs from the :so_user in the params of the YAML file.

Returns:

  • (Boolean)

137
138
139
# File 'lib/specify/configuration/db_config.rb', line 137

def changed_session_user?
  params[:sp_user] != session_user
end

#changed_user?Boolean

-> true or false Returns true if the user_name in memory differs from the :name in the params of the YAML file.

Returns:

  • (Boolean)

116
117
118
# File 'lib/specify/configuration/db_config.rb', line 116

def changed_user?
  params[:db_user][:name] != user_name
end

#connectionObject

-> Hash Returns the connection paramaters for the database as a Hash.


27
28
29
30
31
32
33
# File 'lib/specify/configuration/db_config.rb', line 27

def connection
  raise "#{database} on #{host} not configured" unless known?
  { host: host,
    port: port || 3306,
    user: user_name,
    password: @user_password }
end

#db_userObject

-> Hash Returns a Hash with the MySQL/MariaDB user name and password.


44
45
46
# File 'lib/specify/configuration/db_config.rb', line 44

def db_user
  { name: @user_name, password: @user_password }
end

#host?Boolean

-> true or false Returns true if the host is known (has been configured), false otherwise.

Returns:

  • (Boolean)

58
59
60
# File 'lib/specify/configuration/db_config.rb', line 58

def host?
  hosts[host]
end

#known?Boolean

-> true or false Returns true if the database is known (has been configured), false otherwise.

Returns:

  • (Boolean)

65
66
67
# File 'lib/specify/configuration/db_config.rb', line 65

def known?
  params ? true : false
end

#paramsObject

-> Hash Returns a Hash with the parameters for the current host and database from the configuration YAML file.


72
73
74
# File 'lib/specify/configuration/db_config.rb', line 72

def params
  super.dig:hosts, @host, :databases, @database
end

#saveObject

-> true Saves the current state to the YAML file.


86
87
88
89
90
# File 'lib/specify/configuration/db_config.rb', line 86

def save
  return true if saved?
  host? ? update_host : save_new_host
  super
end

#user_password=(password) ⇒ Object

Sets the MySQL/MariaDB user_password. password: String


108
109
110
111
# File 'lib/specify/configuration/db_config.rb', line 108

def user_password=(password)
  @user_password = password
  touch
end