Class: ProjectdxPipeline::DatabaseConfigurationTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/projectdx_pipeline/database_configuration_template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_file = nil) ⇒ DatabaseConfigurationTemplate

Instantiates a new configuration template. If a template_file is specified, it will be loaded as a YAML file and used to read in the configuration data for the template.



48
49
50
51
52
53
54
55
56
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 48

def initialize(template_file = nil)
  @config_data = {}
  if template_file
    @config_data = self.class.read_template(template_file)
    @client_id = @config_data.delete('client_id')
    @database_prefix = @config_data.delete('database_prefix')
    @environments = @config_data.delete('environments')
  end
end

Instance Attribute Details

#client_idObject

:nodoc:



43
44
45
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 43

def client_id
  @client_id
end

#config_dataObject

the common database config data shared by all environments



30
31
32
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 30

def config_data
  @config_data
end

#database_prefixObject

a string that will prefix the name of each database



36
37
38
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 36

def database_prefix
  @database_prefix
end

#environmentsObject

an array containing the names of each database environment to be configured



33
34
35
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 33

def environments
  @environments
end

Class Method Details

.read_template(template_file) ⇒ Object

:nodoc:



89
90
91
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 89

def self.read_template(template_file) #:nodoc:
  YAML.load_file(template_file)
end

Instance Method Details

#configurationObject

returns a Hash containing the ActiveRecord configuration data for all environments



60
61
62
63
64
65
66
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 60

def configuration
  environments.inject({}) do |config, env_name|
    config[env_name] = @config_data.dup
    config[env_name]['database'] = database_name(env_name)
    config
  end
end

#database_name(environment) ⇒ Object

:nodoc:



76
77
78
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 76

def database_name(environment) #:nodoc:
  "#{database_prefix}-#{environment}-#{client_id}"
end

#render_to_file(path) ⇒ Object

Writes the ActiveRecord configuration data (i.e. config/database.yml) to the specified file path



70
71
72
73
74
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 70

def render_to_file(path)
  File.open(path, 'w') do |f|
    YAML.dump(configuration, f)
  end
end