From 40036952b97734f3ca95c9a4594da29c8f1cb6b9 Mon Sep 17 00:00:00 2001 From: Simon Fish Date: Fri, 22 Mar 2024 14:44:09 +0000 Subject: [PATCH] Default to Rails.env for env, don't double-write constants if avoidable --- README.md | 12 +++++++++++- lib/nvar.rb | 9 +++++++-- lib/nvar/environment_variable.rb | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cd9a496..604b895 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/nvar.rb b/lib/nvar.rb index b35c533..f18d758 100644 --- a/lib/nvar.rb +++ b/lib/nvar.rb @@ -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. @@ -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) @@ -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 diff --git a/lib/nvar/environment_variable.rb b/lib/nvar/environment_variable.rb index e590ef6..661617b 100644 --- a/lib/nvar/environment_variable.rb +++ b/lib/nvar/environment_variable.rb @@ -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?