Module: DWH::Settings

Includes:
Logger
Included in:
Adapters::Adapter
Defined in:
lib/dwh/settings.rb

Overview

Functions related to loading and managing the Adapters settings. These settings should by default have the same name as the adapter but lower case and no camelcase. i.e. MySql adapter has a settings file called mysql.yml.

When creating a new adapter copy settings/basesettings/base.yml and modify it to suit your adapters needs.

An adapters settings are merged into the base settings config. So each adapter doesn't have to define every property when they are the same.

By default the file will be looked for in relative location like settings/myadapter.yml. However, you can specify the locastion with (@see #settings_file_path)

Constant Summary collapse

BASE_SETTINGS_FILE =

This is the default base settings that each adapter can override with its own yaml files.

File.join(__dir__, 'settings', 'base.yml')

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger, logger

Instance Attribute Details

#adapter_settingsObject (readonly)

Gets the current loaded adatper settings. If nil, then load_settings hasn't been called.



22
23
24
# File 'lib/dwh/settings.rb', line 22

def adapter_settings
  @adapter_settings
end

Instance Method Details

#adapter_nameObject



75
76
77
# File 'lib/dwh/settings.rb', line 75

def adapter_name
  name.split('::').last.downcase
end

#load_settingsObject

This will load adapter level settings. These settings can be overridden at runtime by calling alter_settings after an adapter is initialized.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dwh/settings.rb', line 31

def load_settings
  return unless @adapter_settings.nil?

  logger.debug "+++ LOADING SETTINGS: #{name} +++"

  @using_base = true
  @adapter_settings = YAML.load_file(BASE_SETTINGS_FILE)

  if File.exist?(settings_file)
    @using_base = false
    settings_from_file = YAML.load_file(settings_file) || {}
    @adapter_settings.merge!(settings_from_file)
  else
    logger.debug "#{adapter_name} Adapter didn't have a settings YAML file. Using only base settings."
  end

  @adapter_settings.transform_keys! do |key|
    key.to_sym
  rescue StandardError
    key
  end
end

#settings_fileObject

By default settings_file are expected to be in a relative directory called settings. If not, change the settings file with call to settings_file_path FILE_PATH



57
58
59
# File 'lib/dwh/settings.rb', line 57

def settings_file
  @settings_file ||= File.join(__dir__, 'settings', "#{adapter_name}.yml")
end

#settings_file_path(file) ⇒ Object

Allows the manual configuration of where to load default database settings from.

It will reload settings if adapter settings has already been loaded.

Parameters:

  • file (String)
    • path or file name string


67
68
69
70
71
72
73
# File 'lib/dwh/settings.rb', line 67

def settings_file_path(file)
  @settings_file = file
  return if @adapter_settings.nil?

  @adapter_settings = nil
  load_settings
end

#using_base_settings?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/dwh/settings.rb', line 79

def using_base_settings?
  @using_base
end