class Mimi::Config

Config stores the manifest and reads and stores configurable parameters from ENV.

@see README.md

Constants

VERSION

Attributes

params[R]

Current set of values for configurable and const parameters

Public Class Methods

load_manifest(filename, _opts = {}) click to toggle source

Reads and parses the manifest file and constructs the Mimi::Core::Manifest object

@param filename [Pathname] @param _opts @return [Mimi::Core::Manifest]

# File lib/mimi/config.rb, line 159
def self.load_manifest(filename, _opts = {})
  Mimi::Core::Manifest.from_yaml(File.read(filename))
rescue StandardError => e
  raise "Failed to load config manifest from '#{filename}': #{e}"
end
manifest() click to toggle source

Mimi::Config module manifest

# File lib/mimi/config.rb, line 27
def self.manifest
  {
    config_raise_on_missing_params: {
      desc: 'Raise error on missing params',
      default: true,
      hidden: true
    },
    config_use_dotenv: {
      desc: 'Use Dotenv and load .env file',
      default: true,
      hidden: true
    }
  }
end
module_path() click to toggle source

Returns the module path, for exported rake files

@return [Pathname]

# File lib/mimi/config.rb, line 46
def self.module_path
  Pathname.new(__dir__).join('..', '..').expand_path
end
new(manifest_filename = nil, opts = {}) click to toggle source

Creates a Config object.

Loads and parses manifest.yml, reads and sets configurable parameters from ENV.

Raises an error if any of the required configurable parameters are missing.

@param manifest_filename [String,nil] path to the manifest.yml or nil to skip loading manifest

# File lib/mimi/config.rb, line 59
def initialize(manifest_filename = nil, opts = {})
  @manifest = Mimi::Core::Manifest.new({})
  @params = {}
  load(manifest_filename, opts) if manifest_filename
end

Public Instance Methods

[](key) click to toggle source

Returns the parameter value

@param key [Symbol] parameter name

# File lib/mimi/config.rb, line 107
def [](key)
  unless key.is_a?(Symbol)
    raise ArgumentError, "Invalid key to #[], Symbol expected: '#{key.inspect}'"
  end
  raise ArgumentError, "Undefined parameter '#{key}'" unless include?(key)
  @params[key]
end
include?(name) click to toggle source

Returns true if the config manifest includes the parameter with the given name.

If manifest includes the parameter name, it is safe to access paramter via [] and #<name> methods.

# File lib/mimi/config.rb, line 99
def include?(name)
  @manifest.to_h.key?(name.to_sym)
end
load(manifest_filename, opts = {}) click to toggle source

Loads and parses manifest.yml, reads and sets configurable parameters from ENV.

# File lib/mimi/config.rb, line 68
def load(manifest_filename, opts = {})
  opts = self.class.options.deep_merge(opts)
  manifest_filename = Pathname.new(manifest_filename).expand_path
  @manifest.merge!(self.class.load_manifest(manifest_filename, opts))
  load_params(opts)
  if opts[:config_raise_on_missing_params] && !missing_params.empty?
    raise "Missing required configurable parameters: #{missing_params.join(', ')}"
  end
  self
end
manifest() click to toggle source

Returns the underlying manifest

@return [Mimi::Core::Manifest]

# File lib/mimi/config.rb, line 90
def manifest
  @manifest
end
method_missing(name, *) click to toggle source

Provides access to parameters as methods.

Example:

config[:foo] # => 'bar'
config.foo # => 'bar'

# missing parameter
config[:bar] # => ArgumentError
config.bar # => NoMethodError
Calls superclass method
# File lib/mimi/config.rb, line 125
def method_missing(name, *)
  return self[name] if include?(name)
  super
end
missing_params() click to toggle source

Returns list of missing required params

# File lib/mimi/config.rb, line 81
def missing_params
  required_params = manifest.keys.select { |name| manifest.required?(name) }
  required_params - @params.keys
end
respond_to_missing?(name, *) click to toggle source
Calls superclass method
# File lib/mimi/config.rb, line 130
def respond_to_missing?(name, *)
  include?(name) || super
end
to_h() click to toggle source

Returns Hash representation of the config. All Hash keys are Symbol

@return [Hash]

# File lib/mimi/config.rb, line 139
def to_h
  @manifest.to_h.keys.map do |k|
    [k, self[k]]
  end.to_h
end
to_s() click to toggle source

Returns to_h.to_s

@return [String]

# File lib/mimi/config.rb, line 149
def to_s
  to_h.to_s
end

Private Instance Methods

load_params(opts = {}) click to toggle source

Reads parameters from the ENV according to the current manifest

# File lib/mimi/config.rb, line 169
def load_params(opts = {})
  Dotenv.load if opts[:config_use_dotenv]
  @params = @manifest.apply(ENV.to_h.symbolize_keys)
end