Class: Ballast::Configuration

Inherits:
HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/ballast/configuration.rb

Overview

A class which loads a list of YAML files in a folder and expose them in a dotted notation. For each file, only the subsection for the current environment is loaded, so each YAML document should be an hash.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*sections, root: nil, environment: nil) ⇒ Configuration

Creates a new configuration.

Parameters:

  • sections (Array)

    A list of sections to load. Each section name should be the basename (without extension) of a file in the root folder. Subfolders are not supported.

  • root (String|NilClass) (defaults to: nil)

    The root folder where look for file.

  • environment (String|NilClass) (defaults to: nil)

    The environment to load.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ballast/configuration.rb', line 30

def initialize(*sections, root: nil, environment: nil)
  super()
  root ||= ::Ballast::Configuration.default_root
  environment ||= ::Ballast::Configuration.default_environment

  sections.each do |section|
    content = load_section(root, section)
    self[section.underscore] = content.fetch(environment, {})
  end

  enable_dotted_access
end

Class Method Details

.default_environmentString

Returns the default environment. It will be the first non-nil of the following: Rails environment, the Rack environment or "production".

Returns:

  • (String)

    The default environment.



20
21
22
# File 'lib/ballast/configuration.rb', line 20

def self.default_environment
  defined?(Rails) ? Rails.env : ENV.fetch("RACK_ENV", "production")
end

.default_rootString

Returns the default root directory to lookup a configuration. It will be the Rails root if set or the current folder.

Returns:

  • (String)

    The default root directory to lookup a configuration.



13
14
15
# File 'lib/ballast/configuration.rb', line 13

def self.default_root
  defined?(Rails) ? Rails.root.to_s : Dir.pwd
end