1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/lib/active_support/environment_inquirer.rb
Charles Oliver Nutter 09e0372532
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.
2020-03-05 14:57:27 -06:00

20 lines
482 B
Ruby

# 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