Merge pull request #38657 from headius/optimize_string_inquirer

Use optimized subclass of StringInquirer for Rails.env
This commit is contained in:
Rafael França 2020-03-05 17:13:15 -05:00 committed by GitHub
commit fb3ab475ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 2 deletions

View File

@ -70,6 +70,7 @@ module ActiveSupport
autoload :OrderedHash
autoload :OrderedOptions
autoload :StringInquirer
autoload :EnvironmentInquirer
autoload :TaggedLogging
autoload :XmlMini
autoload :ArrayInquirer

View File

@ -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 <tt>ActiveSupport::StringInquirer</tt> class,

View File

@ -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

View File

@ -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: