lib/envied.rb in envied-0.10.0.alpha1 vs lib/envied.rb in envied-0.10.0.alpha3

- old
+ new

@@ -14,10 +14,11 @@ end def self.require(*args, **options) requested_groups = (args && !args.empty?) ? args : ENV['ENVIED_GROUPS'] env!(requested_groups, options) + error_on_duplicate_variables!(options) error_on_missing_variables!(options) error_on_uncoercible_variables!(options) intercept_env_vars! ensure_spring_after_fork_require(args, options) @@ -26,12 +27,24 @@ def self.env!(requested_groups, **options) @config = options.fetch(:config) { Configuration.load } @env = EnvProxy.new(@config, groups: required_groups(*requested_groups)) end + def self.error_on_duplicate_variables!(options) + var_types_by_name = env.variables.reduce({}) do |acc, v| + (acc[v.name] ||= []).push v.type + acc + end + dups = var_types_by_name.select {|name, types| types.uniq.count > 1 } + + if dups.any? + raise "The following variables are defined more than once with different types: #{dups.keys.join(', ')}" + end + end + def self.error_on_missing_variables!(**options) - names = env.missing_variables.map(&:name) + names = env.missing_variables.map(&:name).uniq.sort if names.any? msg = "The following environment variables should be set: #{names.join(', ')}." msg << "\nPlease make sure to stop Spring before retrying." if spring_enabled? && !options[:via_spring] raise msg end @@ -53,10 +66,10 @@ result = groups.compact.map(&splitter).flatten result.any? ? result.map(&:to_sym) : [:default] end def self.env_keys_to_intercept - config.variables.select {|var| var.type == :env }.map{|var| var.name.to_s } + env.variables.select{|v| v.type == :env }.map{|var| var.name.to_s } end def self.intercept_env_vars! if (keys = env_keys_to_intercept).any? ENV.instance_eval { @__envied_env_keys = keys }