From 09e037253238c61fdf587315ecdff8d56dca7d83 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 26 Nov 2019 16:56:14 -0600 Subject: [PATCH] Use optimized subclass of StringInquirer for Rails.env This adds a StringInquirer subclass EnvironmentInquirer that predefines the three default environments as query methods, in order to avoid dispatching through `method_missing` for every call to those methods. The original StringInquirer was not modified due to the side effects of having new env-related methods on it. This new class was not implemented using lazy method definition to avoid the open-ended possibility of defining a new method for all query calls. The three default environments should cover a high percentage of real-world uses, and users with custom environments could add their own to this class. Fixes #37803. --- activesupport/lib/active_support.rb | 1 + .../active_support/core_ext/string/inquiry.rb | 1 + .../active_support/environment_inquirer.rb | 20 +++++++++++++++++++ railties/lib/rails.rb | 4 ++-- 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 activesupport/lib/active_support/environment_inquirer.rb diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index ba31f392cc..03a9aa007d 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -70,6 +70,7 @@ module ActiveSupport autoload :OrderedHash autoload :OrderedOptions autoload :StringInquirer + autoload :EnvironmentInquirer autoload :TaggedLogging autoload :XmlMini autoload :ArrayInquirer diff --git a/activesupport/lib/active_support/core_ext/string/inquiry.rb b/activesupport/lib/active_support/core_ext/string/inquiry.rb index a796d5fb4f..d78ad9b741 100644 --- a/activesupport/lib/active_support/core_ext/string/inquiry.rb +++ b/activesupport/lib/active_support/core_ext/string/inquiry.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "active_support/string_inquirer" +require "active_support/environment_inquirer" class String # Wraps the current string in the ActiveSupport::StringInquirer class, diff --git a/activesupport/lib/active_support/environment_inquirer.rb b/activesupport/lib/active_support/environment_inquirer.rb new file mode 100644 index 0000000000..05361d9327 --- /dev/null +++ b/activesupport/lib/active_support/environment_inquirer.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "active_support/string_inquirer" + +module ActiveSupport + class EnvironmentInquirer < StringInquirer #:nodoc: + DEFAULT_ENVIRONMENTS = ["development", "test", "production"] + def initialize(env) + super(env) + + DEFAULT_ENVIRONMENTS.each do |default| + instance_variable_set :"@#{default}", env == default + end + end + + DEFAULT_ENVIRONMENTS.each do |env| + class_eval "def #{env}?; @#{env}; end" + end + end +end diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index 0b2a7858cb..15037ac984 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -70,14 +70,14 @@ module Rails # Rails.env.development? # => true # Rails.env.production? # => false def env - @_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development") + @_env ||= ActiveSupport::EnvironmentInquirer.new(ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development") end # Sets the Rails environment. # # Rails.env = "staging" # => "staging" def env=(environment) - @_env = ActiveSupport::StringInquirer.new(environment) + @_env = ActiveSupport::EnvironmentInquirer.new(environment) end # Returns all Rails groups for loading based on: