Skip to content

Commit

Permalink
Default to Rails.env for env, don't double-write constants if avoidable
Browse files Browse the repository at this point in the history
  • Loading branch information
boardfish committed Mar 22, 2024
1 parent 8e125c4 commit 4003695
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ If your app relies on lots of environment secrets, onboarding's tough. New team
You can use `Nvar` in Ruby apps, with out-of-the-box support provided for Rails.
## Installation

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

It's recommended to do this by adding the following to `config/application.rb`:

```rb
require "dotenv/load" # if using in tandem with dotenv
require "nvar"

Nvar.load_all
```

## Configuration

`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.
Expand Down
9 changes: 7 additions & 2 deletions lib/nvar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
module Nvar
mattr_accessor :config_file_path, default: File.expand_path("config/environment_variables.yml")
mattr_accessor :env_file_path, default: File.expand_path(".env")
mattr_accessor :env, default: :development
mattr_accessor :env

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

class << self
def env
ActiveSupport::StringInquirer.new(@@env.to_s)
if defined?(Rails)
Rails.env
else
ActiveSupport::StringInquirer.new(@@env&.to_s || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development")
end
end

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

def load_all
Expand Down
2 changes: 1 addition & 1 deletion lib/nvar/environment_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize(name:, type: "String", filter_from_requests: nil, **args)
def to_const
raise Nvar::EnvironmentVariableNotPresentError, self unless defined

Object.const_set(name, typecast_value)
Object.const_set(name, typecast_value) unless Object.const_defined?(name)
end

def set?
Expand Down

0 comments on commit 4003695

Please sign in to comment.