From 4754bd8be7de6a83fe7e09a39ceb3476b95175f5 Mon Sep 17 00:00:00 2001 From: Manjunath Date: Sun, 10 Oct 2021 19:02:05 +0530 Subject: [PATCH] DRY - Code Refactor --- lib/config/integrations/heroku.rb | 20 +++---- lib/config/options.rb | 88 ++++++++++++++----------------- lib/config/rack/reloader.rb | 4 +- lib/config/sources/env_source.rb | 12 ++--- lib/config/sources/yaml_source.rb | 2 +- 5 files changed, 59 insertions(+), 67 deletions(-) diff --git a/lib/config/integrations/heroku.rb b/lib/config/integrations/heroku.rb index 8e41bc73..9ed6e746 100644 --- a/lib/config/integrations/heroku.rb +++ b/lib/config/integrations/heroku.rb @@ -41,16 +41,16 @@ def `(command) def to_dotted_hash(source, target = {}, namespace = nil) prefix = "#{namespace}." if namespace case source - when Hash - source.each do |key, value| - to_dotted_hash(value, target, "#{prefix}#{key}") - end - when Array - source.each_with_index do |value, index| - to_dotted_hash(value, target, "#{prefix}#{index}") - end - else - target[namespace] = source + when Hash + source.each do |key, value| + to_dotted_hash(value, target, "#{prefix}#{key}") + end + when Array + source.each_with_index do |value, index| + to_dotted_hash(value, target, "#{prefix}#{index}") + end + else + target[namespace] = source end target end diff --git a/lib/config/options.rb b/lib/config/options.rb index 40e49f6a..9d435c7e 100644 --- a/lib/config/options.rb +++ b/lib/config/options.rb @@ -15,20 +15,11 @@ def empty? end def add_source!(source) - # handle yaml file paths - source = (Sources::YAMLSource.new(source)) if source.is_a?(String) - source = (Sources::HashSource.new(source)) if source.is_a?(Hash) - - @config_sources ||= [] - @config_sources << source + load_config_sources(:push, source) end def prepend_source!(source) - source = (Sources::YAMLSource.new(source)) if source.is_a?(String) - source = (Sources::HashSource.new(source)) if source.is_a?(Hash) - - @config_sources ||= [] - @config_sources.unshift(source) + load_config_sources(:unshift, source) end # look through all our sources and rebuild the configuration @@ -40,15 +31,7 @@ def reload! if conf.empty? conf = source_conf else - DeepMerge.deep_merge!( - source_conf, - conf, - preserve_unmergeables: false, - knockout_prefix: Config.knockout_prefix, - overwrite_arrays: Config.overwrite_arrays, - merge_nil_values: Config.merge_nil_values, - merge_hash_arrays: Config.merge_hash_arrays - ) + hash_deep_merge(source_conf, conf) end end @@ -69,14 +52,8 @@ def reload_from_files(*files) def to_hash result = {} - marshal_dump.each do |k, v| - if v.instance_of? Config::Options - result[k] = v.to_hash - elsif v.instance_of? Array - result[k] = descend_array(v) - else - result[k] = v - end + marshal_dump.each do |key, value| + result[key] = instance_of(value) end result end @@ -98,15 +75,7 @@ def as_json(options = nil) def merge!(hash) current = to_hash - DeepMerge.deep_merge!( - hash.dup, - current, - preserve_unmergeables: false, - knockout_prefix: Config.knockout_prefix, - overwrite_arrays: Config.overwrite_arrays, - merge_nil_values: Config.merge_nil_values, - merge_hash_arrays: Config.merge_hash_arrays - ) + hash_deep_merge(hash.dup, current) marshal_load(__convert(current).marshal_dump) self end @@ -152,23 +121,46 @@ def respond_to_missing?(*args) protected - def descend_array(array) - array.map do |value| - if value.instance_of? Config::Options - value.to_hash - elsif value.instance_of? Array - descend_array(value) - else - value - end + def load_config_sources(method, source) + # handle yaml file paths + source = (Sources::YAMLSource.new(source)) if source.is_a?(String) + source = (Sources::HashSource.new(source)) if source.is_a?(Hash) + + @config_sources ||= [] + @config_sources.send(method, source) + end + + def hash_deep_merge(source, dest) + DeepMerge.deep_merge!( + source, + dest, + preserve_unmergeables: false, + knockout_prefix: Config.knockout_prefix, + overwrite_arrays: Config.overwrite_arrays, + merge_nil_values: Config.merge_nil_values, + merge_hash_arrays: Config.merge_hash_arrays + ) + end + + def instance_of(value) + if value.instance_of? Config::Options + value.to_hash + elsif value.instance_of? Array + descend_array(value) + else + value end end + def descend_array(array) + array.map { |value| instance_of(value) } + end + # Recursively converts Hashes to Options (including Hashes inside Arrays) - def __convert(h) #:nodoc: + def __convert(hash) #:nodoc: s = self.class.new - h.each do |k, v| + hash.each do |k, v| k = k.to_s if !k.respond_to?(:to_sym) && k.respond_to?(:to_s) if v.is_a?(Hash) diff --git a/lib/config/rack/reloader.rb b/lib/config/rack/reloader.rb index a98c0038..6b8f799a 100644 --- a/lib/config/rack/reloader.rb +++ b/lib/config/rack/reloader.rb @@ -1,6 +1,6 @@ module Config module Rack - # Rack middleware the reloads Config on every request (only use in dev mode) + # Rack middleware that reloads Config on every request (only use in dev mode) class Reloader def initialize(app) @app = app @@ -12,4 +12,4 @@ def call(env) end end end -end \ No newline at end of file +end diff --git a/lib/config/sources/env_source.rb b/lib/config/sources/env_source.rb index df59e35e..1f0dc451 100644 --- a/lib/config/sources/env_source.rb +++ b/lib/config/sources/env_source.rb @@ -31,12 +31,12 @@ def load keys.map! { |key| case converter - when :downcase then - key.downcase - when nil then - key - else - raise "Invalid ENV variables name converter: #{converter}" + when :downcase + key.downcase + when nil + key + else + raise "Invalid ENV variables name converter: #{converter}" end } diff --git a/lib/config/sources/yaml_source.rb b/lib/config/sources/yaml_source.rb index bd656d0a..8e89711d 100644 --- a/lib/config/sources/yaml_source.rb +++ b/lib/config/sources/yaml_source.rb @@ -9,7 +9,7 @@ class YAMLSource def initialize(path, evaluate_erb: Config.evaluate_erb_in_yaml) @path = path.to_s - @evaluate_erb = !!evaluate_erb + @evaluate_erb = evaluate_erb end # returns a config hash from the YML file