Sha256: fee704c84c71ecb6fbacd7179224a5b2291da58de55f9b155f3ebc2e5b90443b
Contents?: true
Size: 1.74 KB
Versions: 2
Compression:
Stored size: 1.74 KB
Contents
require 'yaml' require 'erb' require 'ostruct' class Mona::Config::Loader include Mona::Mixins DEFAULT_CONFIG_FILENAME = 'default.yml' sig do params( config_name: T.any(String, Symbol), ) .returns(Mona::Config) end def call(config_name) config_name = config_name.to_s config = Mona::Config.new default_config_filename = File.join( Mona.current_project.root_path, Mona.current_project.configs_dir, config_name, DEFAULT_CONFIG_FILENAME ) unless File.exist?(default_config_filename) raise StandardError.new("Config '#{config_name}' was not found. Add it to '#{default_config_filename}'") end hash = begin YAML.load( ERB.new(File.read(default_config_filename)).result ) rescue => e puts ERB.new(File.read(default_config_filename)).result.inspect end hash_to_config( hash: hash, config: config ) env_config_filename = File.join( Mona.current_project.root_path, Mona.current_project.configs_dir, config_name, "#{Mona.current_project.env}.yml" ) hash_to_config( hash: YAML.load( ERB.new(File.read(env_config_filename)).result ), config: config ) if File.exist?(env_config_filename) config.send(config_name) end private def hash_to_config(hash:, config:) hash.each do |key, value| if value.is_a?(Hash) nested_config = config.read_attribute(key) if nested_config.nil? || !nested_config.is_a?(Mona::Config) nested_config = Mona::Config.new end hash_to_config(hash: value, config: nested_config) config.write_attribute(key, nested_config) else config.write_attribute(key, value) end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
mona-0.2.3 | lib/mona/config/loader.rb |
mona-0.2.2 | lib/mona/config/loader.rb |