Skip to content

Commit

Permalink
Remove dependency on ActiveSupport
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
BrianBorge committed Jan 28, 2020
1 parent 9a966c9 commit ed555e6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/consult.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

require 'pathname'
require 'yaml'
require 'active_support/core_ext/hash'
require 'erb'
require 'vault'
require 'diplomat'

require 'consult/version'
require 'consult/utilities'
require 'consult/template'
require_relative './support/hash_extensions'

module Consult
@config = {}
Expand Down Expand Up @@ -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|
Expand Down
34 changes: 34 additions & 0 deletions lib/support/hash_extensions.rb
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit ed555e6

Please sign in to comment.