class Mimi::Config
Config
stores the manifest and reads and stores configurable parameters from ENV.
@see README.md
Constants
- VERSION
Attributes
Current set of values for configurable and const parameters
Public Class Methods
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
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
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
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
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
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
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
Returns the underlying manifest
@return [Mimi::Core::Manifest]
# File lib/mimi/config.rb, line 90 def manifest @manifest end
Provides access to parameters as methods.
Example:
config[:foo] # => 'bar' config.foo # => 'bar' # missing parameter config[:bar] # => ArgumentError config.bar # => NoMethodError
# File lib/mimi/config.rb, line 125 def method_missing(name, *) return self[name] if include?(name) super end
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
# File lib/mimi/config.rb, line 130 def respond_to_missing?(name, *) include?(name) || super end
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
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
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