From c4430c2d5364e0925208a34c8ecc60f38564cd64 Mon Sep 17 00:00:00 2001 From: Brian Borge Date: Mon, 27 Jan 2020 19:18:42 -0500 Subject: [PATCH] Remove dependency on ActiveSupport - One less dependency to manage - Smaller deployment package for projects that depend on Consult - Replaces String#present? by converting values to string and then checking that length is greater than 0 (nil.to_s is an empty string) - Replaces Hash#deep_symbolize_keys! and Hash#deep_merge with their ActiveSupport implementations --- consult.gemspec | 1 - lib/consult.rb | 4 ++-- lib/support/hash_extensions.rb | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 lib/support/hash_extensions.rb diff --git a/consult.gemspec b/consult.gemspec index c46c14c..14dcc43 100644 --- a/consult.gemspec +++ b/consult.gemspec @@ -24,7 +24,6 @@ Gem::Specification.new do |spec| spec.executables = ['consult'] spec.require_paths = ['lib'] - spec.add_dependency 'activesupport', '> 4', '< 6' spec.add_dependency 'diplomat', '~> 2.0.2' spec.add_dependency 'vault', '>= 0.10.0', '< 1.0.0' diff --git a/lib/consult.rb b/lib/consult.rb index a0e8b7d..9d30471 100644 --- a/lib/consult.rb +++ b/lib/consult.rb @@ -2,7 +2,6 @@ require 'pathname' require 'yaml' -require 'active_support/core_ext/hash' require 'erb' require 'vault' require 'diplomat' @@ -10,6 +9,7 @@ require 'consult/version' require 'consult/utilities' require 'consult/template' +require_relative './support/hash_extensions' module Consult @config = {} @@ -45,7 +45,7 @@ def configure_consul @config[:consul][:url] = ENV['CONSUL_HTTP_ADDR'] || configured_address || @config[:consul][:url] # If a consul token exists, treat it as special # See https://github.com/WeAreFarmGeek/diplomat/pull/160 - (@config[:consul][:options] ||= {}).merge!(headers: {'X-Consul-Token' => consul_token}) if consul_token.present? + (@config[:consul][:options] ||= {}).merge!(headers: {'X-Consul-Token' => consul_token}) if consul_token.to_s.length > 0 Diplomat.configure do |c| @config[:consul].each do |opt, val| diff --git a/lib/support/hash_extensions.rb b/lib/support/hash_extensions.rb new file mode 100644 index 0000000..864eb0e --- /dev/null +++ b/lib/support/hash_extensions.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +# ActiveSupport's corresponding methods +# - https://github.com/rails/rails/tree/master/activesupport/lib/active_support/core_ext/hash +module HashExtension + def deep_symbolize_keys!(object = self) + case object + when Hash + object.keys.each do |key| + value = object.delete key + object[key.to_sym] = deep_symbolize_keys! value + end + object + when Array + object.map! { |e| deep_symbolize_keys! e } + else + object + end + end + + def deep_merge(other_hash, &block) + merge(other_hash) do |key, this_val, other_val| + if this_val.is_a?(Hash) && other_val.is_a?(Hash) + this_val.deep_merge other_val, &block + elsif block_given? + block.call key, this_val, other_val + else + other_val + end + end + end +end + +Hash.send(:include, HashExtension)