Class: Specify::Configuration::Config

Inherits:
Object
  • Object
show all
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

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

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.

Yields:

  • (_self)

Yield Parameters:



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

#hostsObject (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.

Yields:

  • (db)


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

#paramsObject

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

#saveObject

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.

Returns:

  • (Boolean)


101
102
103
# File 'lib/specify/configuration/config.rb', line 101

def saved?
  @saved
end

#touchObject

Marks the instance as modified.



106
107
108
# File 'lib/specify/configuration/config.rb', line 106

def touch
  @saved = false
end