lib/anyway/env.rb in anyway_config-0.5.1.rc1 vs lib/anyway/env.rb in anyway_config-0.5.1

- old
+ new

@@ -1,7 +1,5 @@ -# frozen_string_literal: true - module Anyway # Parses environment variables and provides # method-like access class Env # Regexp to detect array values @@ -9,33 +7,38 @@ # and doesn't start/end with quote ARRAY_RXP = /\A[^'"].*\s*,\s*.*[^'"]\z/ def initialize @data = {} + load end + def reload + clear + load + self + end + def clear @data.clear + self end - def fetch(config_name) - @data[config_name] ||= parse_env(config_name) + def method_missing(method_name, *args, &_block) + method_name = method_name.to_s.gsub(/\_/, '') + return @data[method_name] if args.empty? && @data.key?(method_name) end private - def parse_env(config_name) - config_env_name = config_name.to_s.delete("_") - config_env_name.upcase! - data = {} + def load ENV.each_pair do |key, val| - if key.start_with?(config_env_name) - _mod, path = extract_module_path(key) - set_by_path(data, path, serialize_val(val)) + if config_key?(key) + mod, path = extract_module_path(key) + set_by_path(get_hash(@data, mod), path, serialize_val(val)) end end - data end def config_key?(key) key =~ /^[A-Z\d]+\_[A-Z\d\_]+/ end @@ -56,12 +59,10 @@ def get_hash(from, name) (from[name] ||= {}) end - # rubocop:disable Metrics/MethodLength - # rubocop:disable Metrics/CyclomaticComplexity def serialize_val(value) case value when ARRAY_RXP value.split(/\s*,\s*/).map(&method(:serialize_val)) when /\A(true|t|yes|y)\z/i @@ -78,9 +79,7 @@ value.gsub(/(\A['"]|['"]\z)/, '') else value end end - # rubocop:enable Metrics/MethodLength - # rubocop:enable Metrics/CyclomaticComplexity end end