Class: Specify::Configuration::Config

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

Overview

A class that wraps a yml .rc file

Direct Known Subclasses

DBConfig, HostConfig

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, dir_names: nil, hosts: nil) {|_self| ... } ⇒ Config

-> Configuration::Config Creates a new instance file: the YAML file (path) containg the configuration dir_names: a Hash with directory names as keys, hosts as values hosts: a Hash with host configurations

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/specify/configuration/config.rb', line 14

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_namesObject (readonly)

Returns the value of attribute dir_names



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

def dir_names
  @dir_names
end

#hostsObject (readonly)

Returns the value of attribute hosts



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

def hosts
  @hosts
end

Class Method Details

.empty(file, &block) ⇒ Object

-> Configuration::Config Returns a new empty instance that can serve as a template. file: the YAML file (path) containg the configuration



32
33
34
35
36
37
38
39
# File 'lib/specify/configuration/config.rb', line 32

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 database configuration hash to the host configuration's databases key: databasename => { db_user: { name: nil, password: nil } } name: String, the database name host: String, the name of the MySQL/MariaDB host for the database

Yields:

  • (db)


56
57
58
59
60
61
62
63
64
# File 'lib/specify/configuration/config.rb', line 56

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 host configuration hash { hostname => { port: portnumber, databases: {} } } name: String, the host name port: Interger, the port number



45
46
47
48
49
# File 'lib/specify/configuration/config.rb', line 45

def add_host(name, port = nil)
  raise "Host '#{name}' already configured" if hosts[name]
  hosts[name] = { port: port, databases: {} }
  @saved = false
end

#paramsObject

-> Hash Returns a Hash with the contents of the configuration YAML file.



68
69
70
# File 'lib/specify/configuration/config.rb', line 68

def params
  { dir_names: @dir_names, hosts: @hosts }
end

#saveObject

-> true Saves the current contens fo params to the YAML configuration file.



74
75
76
77
78
79
# File 'lib/specify/configuration/config.rb', line 74

def save
  File.open(@file, 'w') do |file|
    file.write(Psych.dump(@params))
  end
  @saved = true
end

#saved?Boolean

-> true or false Returns false if the instance has been modified since the last save.

Returns:

  • (Boolean)


83
84
85
# File 'lib/specify/configuration/config.rb', line 83

def saved?
  @saved
end

#touchObject

Sets the saved state to false



88
89
90
# File 'lib/specify/configuration/config.rb', line 88

def touch
  @saved = false
end