Class: Specify::Configuration::Config
- Inherits:
-
Object
- Object
- Specify::Configuration::Config
- Defined in:
- lib/specify/configuration/config.rb
Overview
Configurations wrap a database configuaratin file (.rc.yaml file).
Configuration is the superclass of the DBConfig and HostConfig classes.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#dir_names ⇒ Object
readonly
A Hash containing the directory-host-mapping parameters for the HostConfig subclass.
-
#hosts ⇒ Object
readonly
A Hash containing the database parameters for the DBConfig subclass.
Class Method Summary collapse
-
.empty(file, &block) ⇒ Object
Returns a new empty Config for
file
that can serve as a template.
Instance Method Summary collapse
-
#add_database(name, host:) {|db| ... } ⇒ Object
Adds a configuration for the database with
name
to thehost
configuration. -
#add_host(name, port = nil) ⇒ Object
Adds a configuration for the host with
name
. -
#initialize(file = nil, dir_names: nil, hosts: nil) {|_self| ... } ⇒ Config
constructor
Returns a new Config for
file
(a YAML file containg the configuration). -
#params ⇒ Object
Returns a Hash with the contents of the configuration YAML file.
-
#save ⇒ Object
Saves the current state to the YAML configuration file.
-
#saved? ⇒ Boolean
Returns
false
if the instance has been modified since the last save. -
#touch ⇒ Object
Marks the instance as modified.
Constructor Details
#initialize(file = nil, dir_names: nil, hosts: nil) {|_self| ... } ⇒ Config
Returns a new Config for file
(a YAML file containg the configuration).
dir_names
: a Hash with directory names as keys, host names as values.
hosts
: a Hash with host configurations. The hash should have the structure:
{
:hosts => {
'hostname' => {
:port => Integer,
:databases => {
'database name' => {
:db_user => {
:name => 'mysql_user_name',
:password => 'password'
},
:sp_user => 'specify_user_name'
}
}
}
}
}
Leave :password
out to be prompted.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/specify/configuration/config.rb', line 53 def initialize(file = nil, dir_names: nil, hosts: nil) @file = Pathname.new(file) if dir_names || hosts @dir_names = dir_names @hosts = hosts @params = { dir_names: dir_names, hosts: hosts } else @params = Psych.load_file(@file) @dir_names = @params[:dir_names] @hosts = @params[:hosts] end yield(self) if block_given? @saved = nil end |
Instance Attribute Details
#dir_names ⇒ Object (readonly)
A Hash containing the directory-host-mapping parameters for the HostConfig subclass.
11 12 13 |
# File 'lib/specify/configuration/config.rb', line 11 def dir_names @dir_names end |
#hosts ⇒ Object (readonly)
A Hash containing the database parameters for the DBConfig subclass.
14 15 16 |
# File 'lib/specify/configuration/config.rb', line 14 def hosts @hosts end |
Class Method Details
.empty(file, &block) ⇒ Object
Returns a new empty Config for file
that can serve as a template.
file
: the YAML file containg the configuration
19 20 21 22 23 24 25 26 |
# File 'lib/specify/configuration/config.rb', line 19 def self.empty(file, &block) if File.exist?(file) raise "#{file} exists, won't overwrite" end config = new file, dir_names: {}, hosts: {}, &block config.save config end |
Instance Method Details
#add_database(name, host:) {|db| ... } ⇒ Object
Adds a configuration for the database with name
to the host
configuration.
70 71 72 73 74 75 76 77 78 |
# File 'lib/specify/configuration/config.rb', line 70 def add_database(name, host:) add_host(host) unless hosts[host] if hosts.dig host, :databases, name raise "Database '#{name}' on '#{host}' already configured" end db = hosts[host][:databases][name] = db_template yield(db) if block_given? @saved = false end |
#add_host(name, port = nil) ⇒ Object
Adds a configuration for the host with name
.
81 82 83 84 85 |
# File 'lib/specify/configuration/config.rb', line 81 def add_host(name, port = nil) raise "Host '#{name}' already configured" if hosts[name] hosts[name] = { port: port, databases: {} } @saved = false end |
#params ⇒ Object
Returns a Hash with the contents of the configuration YAML file.
88 89 90 |
# File 'lib/specify/configuration/config.rb', line 88 def params { dir_names: @dir_names, hosts: @hosts } end |
#save ⇒ Object
Saves the current state to the YAML configuration file.
93 94 95 96 97 98 |
# File 'lib/specify/configuration/config.rb', line 93 def save File.open(@file, 'w') do |file| file.write(Psych.dump(@params)) end @saved = true end |
#saved? ⇒ Boolean
Returns false
if the instance has been modified since the last save.
101 102 103 |
# File 'lib/specify/configuration/config.rb', line 101 def saved? @saved end |
#touch ⇒ Object
Marks the instance as modified.
106 107 108 |
# File 'lib/specify/configuration/config.rb', line 106 def touch @saved = false end |