lib/anyway/config.rb in anyway_config-1.2.0 vs lib/anyway/config.rb in anyway_config-1.3.0
- old
+ new
@@ -33,10 +33,15 @@
return (@config_name = val.to_s) unless val.nil?
@config_name = underscore_name unless defined?(@config_name)
@config_name
end
+ def env_prefix(val = nil)
+ return (@env_prefix = val.to_s) unless val.nil?
+ @env_prefix
+ end
+
# Load config as Hash by any name
#
# Example:
#
# my_config = Anyway::Config.for(:my_app)
@@ -54,31 +59,38 @@
word.downcase!
word
end
end
- attr_reader :config_name
+ attr_reader :config_name, :env_prefix
# Instantiate config with specified name, loads the data and applies overrides
#
# Example:
#
# my_config = Anyway::Config.new(name: :my_app, load: true, overrides: { some: :value })
#
- # rubocop:disable Metrics/LineLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
- def initialize(config_name = nil, do_load = nil, name: nil, load: true, overrides: {})
- unless config_name.nil? && do_load.nil?
- warn "[Deprecated] Positional arguments for Anyway::Config#initialize will be removed in 1.2.0. Use keyword arguments instead: initialize(name:, load:, overrides:)"
- end
- name = config_name unless config_name.nil?
- load = do_load unless do_load.nil?
-
+ # rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/LineLength,Metrics/CyclomaticComplexity
+ def initialize(name: nil, load: true, overrides: {})
@config_name = name || self.class.config_name
+
raise ArgumentError, "Config name is missing" unless @config_name
+
+ if @config_name.to_s.include?('_') && @env_prefix.nil?
+ warn "[Deprecated] As your config_name is #{@config_name}, " \
+ "the prefix `#{@config_name.to_s.delete('_').upcase}` " \
+ "will be used to parse env variables. " \
+ "This behavior is about to change in 1.4.0 (no more deleting underscores). " \
+ "Env prefix can be set explicitly with `env_prefix` method now already " \
+ "(check out the docs), and it will be used as is."
+ end
+
+ @env_prefix = self.class.env_prefix || @config_name.to_s&.delete('_')
+
self.load(overrides) if load
end
- # rubocop:enable Metrics/LineLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
+ # rubocop:enable Metrics/MethodLength,Metrics/AbcSize,Metrics/LineLength,Metrics/CyclomaticComplexity
def reload(overrides = {})
clear
load(overrides)
self
@@ -106,17 +118,17 @@
load_from_file(config)
load_from_env(config)
end
def load_from_file(config)
- config_path = Anyway.env.fetch(config_name).delete('conf') ||
+ config_path = Anyway.env.fetch(env_prefix).delete('conf') ||
"./config/#{config_name}.yml"
config.deep_merge!(parse_yml(config_path) || {}) if config_path && File.file?(config_path)
config
end
def load_from_env(config)
- config.deep_merge!(Anyway.env.fetch(config_name))
+ config.deep_merge!(Anyway.env.fetch(env_prefix))
config
end
def to_h
self.class.config_attributes.each_with_object({}) do |key, obj|