Skip to content

Commit 4003695

Browse files
committed
Default to Rails.env for env, don't double-write constants if avoidable
1 parent 8e125c4 commit 4003695

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@ If your app relies on lots of environment secrets, onboarding's tough. New team
55
You can use `Nvar` in Ruby apps, with out-of-the-box support provided for Rails.
66
## Installation
77

8-
Add the gem to your Gemfile and install it with `bundle add nvar`. If you're not on Rails, you'll need to make sure that `Nvar` is required with `require 'nvar'`, and then manually call `Nvar::EnvironmentVariable.load_all` as early as is appropriate.
8+
Add the gem to your Gemfile and install it with `bundle add nvar`. If you're not on Rails, you'll need to make sure that `Nvar` is required with `require 'nvar'`, and then manually call `Nvar.load_all` as early as is appropriate.
9+
10+
It's recommended to do this by adding the following to `config/application.rb`:
11+
12+
```rb
13+
require "dotenv/load" # if using in tandem with dotenv
14+
require "nvar"
15+
16+
Nvar.load_all
17+
```
18+
919
## Configuration
1020

1121
`Nvar` is configured by way of `config/environment_variables.yml`. If you're on Rails, this file will be created for you automatically. Each key corresponds to the name of a required environment variable, and houses its configuration, all of which is optional.

lib/nvar.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
module Nvar
1212
mattr_accessor :config_file_path, default: File.expand_path("config/environment_variables.yml")
1313
mattr_accessor :env_file_path, default: File.expand_path(".env")
14-
mattr_accessor :env, default: :development
14+
mattr_accessor :env
1515

1616
# Comments in .env files must have a leading '#' symbol. This cannot be
1717
# followed by a space.
@@ -42,7 +42,11 @@ def message
4242

4343
class << self
4444
def env
45-
ActiveSupport::StringInquirer.new(@@env.to_s)
45+
if defined?(Rails)
46+
Rails.env
47+
else
48+
ActiveSupport::StringInquirer.new(@@env&.to_s || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development")
49+
end
4650
end
4751

4852
def configure_for_rails(app)
@@ -51,6 +55,7 @@ def configure_for_rails(app)
5155
[config_file_path, env_file_path].each do |path|
5256
File.open(path, "w") {} unless path.exist? # rubocop:disable Lint/EmptyBlock
5357
end
58+
self.env = Rails.env
5459
end
5560

5661
def load_all

lib/nvar/environment_variable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def initialize(name:, type: "String", filter_from_requests: nil, **args)
2626
def to_const
2727
raise Nvar::EnvironmentVariableNotPresentError, self unless defined
2828

29-
Object.const_set(name, typecast_value)
29+
Object.const_set(name, typecast_value) unless Object.const_defined?(name)
3030
end
3131

3232
def set?

0 commit comments

Comments
 (0)