Skip to content

Commit

Permalink
IndifferentHash monkeypatch warning improvements
Browse files Browse the repository at this point in the history
* Print warning on Rubies that require monkeypatching only
* Print warning to stderr instead of stdout
* Load ActiveSupport during internal testing only
* Add SINATRA_ACTIVESUPPORT_WARNING opt-out environment variable
  • Loading branch information
mwpastore committed Sep 20, 2018
1 parent b7c1064 commit 462803e
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/sinatra/indifferent_hash.rb
@@ -1,21 +1,23 @@
# frozen_string_literal: true
if !$LOAD_PATH.grep(%r{gems/activesupport}).empty? && $LOADED_FEATURES.grep(%r{active_support/core_ext/hash}).empty?
puts <<-EOF
if !Hash.method_defined?(:slice) && !$LOAD_PATH.grep(%r{gems/activesupport}).empty?
if ENV['APP_ENV'] == 'test' && File.basename(File.dirname(ENV['BUNDLE_GEMFILE'])) == 'sinatra'
# Some extensions get loaded during internal testing (e.g. by RABL and our
# RABL test) that we have no control over, but we need them to load
# *before* IndifferentHash, so we'll do it preemptively here.
#
# Newer Rubies have these methods built-in, so the extensions are no-ops.
require 'active_support/core_ext/hash/conversions'
require 'active_support/core_ext/hash/slice'
require 'active_support/core_ext/hash/keys'
elsif ENV['SINATRA_ACTIVESUPPORT_WARNING'] != 'false'
$stderr.puts <<-EOF
WARNING: If you plan to load any of ActiveSupport's core extensions to Hash, be
sure to do so *before* loading Sinatra::Application or Sinatra::Base. If not,
you may disregard this warning.
EOF
end
if ENV['APP_ENV'] == 'test' && !Hash.method_defined?(:slice)
# Some extensions get loaded during testing (e.g. by RABL and our RABL test)
# that we have no control over, but we need it to load *before*
# IndifferentHash, so we'll do it preemptively here.
#
# Newer Rubies have these methods built-in, so the extensions are no-ops.
require 'active_support/core_ext/hash/conversions'
require 'active_support/core_ext/hash/slice'
require 'active_support/core_ext/hash/keys'
Set SINATRA_ACTIVESUPPORT_WARNING=false in the environment to hide this warning.
EOF
end
end

module Sinatra
Expand Down

0 comments on commit 462803e

Please sign in to comment.