Class: CleanConfig::Configuration

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/clean_config/configuration.rb

Overview

Provides access to configuration data Supports both [:property] and ‘.property’ style access to yml configuration Default configuration path is ‘config/config.yml`

Constant Summary collapse

DEFAULT_CONFIGURATION_DIRECTORY =

directory where configuration yml is expected to be, relative to project root directory

'config'
DEFAULT_CONFIGURATION_FILE_NAME =

name of configuration yml

"#{DEFAULT_CONFIGURATION_DIRECTORY}.yml"
CODE_DIRECTORIES =

directories commonly found at project root directory, used to find the project root

%w(lib spec bin)
LOG =

logger

ConfLogger.new(STDOUT)

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Pass along methods not recognized to the underlying data structure



121
122
123
# File 'lib/clean_config/configuration.rb', line 121

def method_missing(method, *args, &block)
  @data_model.send(method, *args, &block)
end

Class Method Details

.find_execution_path(path) ⇒ String

Finds a Ruby project’s lib directory by looking for a Gemfile sibling

Parameters:

  • path (String)

    The path in which to look for the project’s lib directory

Returns:

  • (String)

    the project root directory



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/clean_config/configuration.rb', line 60

def find_execution_path(path)
  path = File.extname(path).empty? ? path : File.dirname(path)
  directories = path.split(File::Separator)
  project_directory = ''

  until directories.nil? || directories.empty?
    if CODE_DIRECTORIES.include?(directories.last) && project_directory.empty?
      directories.pop
      gemfile_location = File.join(directories.join(File::Separator), 'Gemfile')
      project_directory = File.dirname(gemfile_location) if File.exist?(gemfile_location)
    end
    directories.pop
  end
  project_directory
end

.resolve_config_path(calling_file) ⇒ String

Finds the full path to the project’s configuration file

Parameters:

  • calling_file (String)

    path of the file that invoked this code

Returns:

  • (String)

    full path to configuration



48
49
50
51
52
53
54
# File 'lib/clean_config/configuration.rb', line 48

def resolve_config_path(calling_file)
  config_location = File.join(Configuration::DEFAULT_CONFIGURATION_DIRECTORY,
                              Configuration::DEFAULT_CONFIGURATION_FILE_NAME)

  config_path = find_execution_path(calling_file)
  config_path.empty? ? config_path : File.join(config_path, config_location)
end

Instance Method Details

#add!(config_path = nil) ⇒ CleanConfig::Configuration

Allows the user to specify a config file other than ‘config/config.yml’

Parameters:

  • config_path (String) (defaults to: nil)

    provided as a convenience, but should be avoided in favor of the default location

Returns:



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/clean_config/configuration.rb', line 89

def add!(config_path = nil)
  calling_code_file_path = caller.first.split(':').first
  config_path ||= Configuration.resolve_config_path(calling_code_file_path)
  fail FileNotFoundException, "#{config_path} not found" unless File.exist?(config_path)

  LOG.debug("Reading configuration from #{config_path}")
  config_hash = YAML.load_file(config_path)
  fail InvalidConfigException, "YAML unable to parse empty #{config_path}" unless config_hash # empty YAML returns false

  merge!(config_hash)
end

#empty?Boolean

Returns whether the configuration is empty or not

Returns:

  • (Boolean)

    true if configuration is empty



128
129
130
# File 'lib/clean_config/configuration.rb', line 128

def empty?
  @data_model.nil?
end

#load!Configuration

Loads configuration relative to Ruby’s execution directory. Useful for accessing config in tests and Rake tasks

Returns:



81
82
83
# File 'lib/clean_config/configuration.rb', line 81

def load!
  add!(File.join(DEFAULT_CONFIGURATION_DIRECTORY, DEFAULT_CONFIGURATION_FILE_NAME))
end

#merge!(config = {}) ⇒ CleanConfig::Configuration

Set and merge config at runtime without a file

Parameters:

  • config (Hash) (defaults to: {})

    data to add to configuration

Returns:



105
106
107
108
109
# File 'lib/clean_config/configuration.rb', line 105

def merge!(config = {})
  @data_hash = @data_hash.nil? ? config : @data_hash.deep_merge(config)
  @data_model = RecursiveOpenStruct.new(@data_hash, recurse_over_arrays: true)
  self
end

#parse_key(config_key) ⇒ Object

Given a period-delimited string of keys, find the nested value stored in the configuration

Parameters:

  • config_key (String)

    period-delimited string of nested keys

Returns:

  • (Object)

    value retrieved



115
116
117
118
# File 'lib/clean_config/configuration.rb', line 115

def parse_key(config_key)
  fail 'config_key required' if config_key.nil? || config_key.empty?
  parse_key_recursive(@data_model, config_key.split('.').reverse, '')
end

#reset!CleanConfig::Configuration

Clear configuration

Returns:



135
136
137
138
139
# File 'lib/clean_config/configuration.rb', line 135

def reset!
  @data_model = nil
  @data_hash  = nil
  self
end