Class: Dotenv::Rails
- Inherits:
-
Rails::Railtie
- Object
- Rails::Railtie
- Dotenv::Rails
- Defined in:
- lib/dotenv/rails.rb
Overview
Rails integration for using Dotenv to load ENV variables from a file
Constant Summary collapse
- TEST_RAKE_TASKS =
/^(default$|test(:|$)|parallel:spec|spec(:|$))/
Class Method Summary collapse
-
.load ⇒ Object
Rails uses ‘#method_missing` to delegate all class methods to the instance, which means `Kernel#load` gets called here.
Instance Method Summary collapse
-
#deprecator ⇒ Object
:nodoc:.
-
#env ⇒ Object
The current environment that the app is running in.
-
#files ⇒ Object
The list of files to load, joined with Rails.root.
-
#initialize ⇒ Rails
constructor
A new instance of Rails.
-
#load ⇒ Object
Public: Load dotenv.
- #overload ⇒ Object
-
#root ⇒ Object
Internal: ‘Rails.root` is nil in Rails 4.1 before the application is initialized, so this falls back to the `RAILS_ROOT` environment variable, or the current working directory.
Constructor Details
#initialize ⇒ Rails
Returns a new instance of Rails.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/dotenv/rails.rb', line 29 def initialize super() config.dotenv = ActiveSupport::OrderedOptions.new.update( # Rails.logger is not available yet, so we'll save log messages and replay them when it is logger: Dotenv::ReplayLogger.new, overwrite: false, files: [ ".env.#{env}.local", (".env.local" unless env.test?), ".env.#{env}", ".env" ].compact, autorestore: env.test? && !defined?(ClimateControl) && !defined?(IceAge) ) end |
Class Method Details
.load ⇒ Object
Rails uses ‘#method_missing` to delegate all class methods to the instance, which means `Kernel#load` gets called here. We don’t want that.
92 93 94 |
# File 'lib/dotenv/rails.rb', line 92 def self.load instance.load end |
Instance Method Details
#deprecator ⇒ Object
:nodoc:
86 87 88 |
# File 'lib/dotenv/rails.rb', line 86 def deprecator # :nodoc: @deprecator ||= ActiveSupport::Deprecation.new end |
#env ⇒ Object
The current environment that the app is running in.
When running ‘rake`, the Rails application is initialized in development, so we have to check which rake tasks are being run to determine the environment.
76 77 78 79 80 81 82 83 |
# File 'lib/dotenv/rails.rb', line 76 def env @env ||= if defined?(Rake.application) && Rake.application.top_level_tasks.grep(TEST_RAKE_TASKS).any? env = Rake.application..show_tasks ? "development" : "test" ActiveSupport::EnvironmentInquirer.new(env) else ::Rails.env end end |
#files ⇒ Object
The list of files to load, joined with Rails.root
46 47 48 |
# File 'lib/dotenv/rails.rb', line 46 def files config.dotenv.files.map { |file| root.join(file) } end |
#load ⇒ Object
Public: Load dotenv
This will get called during the ‘before_configuration` callback, but you can manually call `Dotenv::Rails.load` if you needed it sooner.
54 55 56 |
# File 'lib/dotenv/rails.rb', line 54 def load Dotenv.load(*files, overwrite: overwrite) end |
#overload ⇒ Object
58 59 60 61 |
# File 'lib/dotenv/rails.rb', line 58 def overload deprecator.warn("Dotenv::Rails.overload is deprecated. Set `Dotenv::Rails.overwrite = true` and call Dotenv::Rails.load instead.") Dotenv.load(*files, overwrite: true) end |
#root ⇒ Object
Internal: ‘Rails.root` is nil in Rails 4.1 before the application is initialized, so this falls back to the `RAILS_ROOT` environment variable, or the current working directory.
66 67 68 |
# File 'lib/dotenv/rails.rb', line 66 def root ::Rails.root || Pathname.new(ENV["RAILS_ROOT"] || Dir.pwd) end |