nvar 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dcd99458bfe71f2186f552eb9c0759108c86ceeed9b9ed405edf34a758d2dfba
4
- data.tar.gz: ec71795cc0dad1e4b8d5ba2e75dfa9c1f6e56d32df73e9244258f62615d67397
3
+ metadata.gz: 50a7afaa683d712f4d58162159ac28c3830b58555fbf6b951024265ef3ca46c1
4
+ data.tar.gz: a61eda6ce582178c41953d12969449c106a82f6fa6a653d62f442e41de0547e2
5
5
  SHA512:
6
- metadata.gz: a1d1b2066a8f922f57a26aebba4c47f5b3a71475360e24899422c0bbb4650fe6ae928fc5c49190d401a43d3ce775f1e8ba91a13212ac22a011b207ddca4e6755
7
- data.tar.gz: 1bb2f8a67579ed8a771c2f827fced75478c5a8db23dc68158ad168579c9b4292546ba6d364675ea983d2d3742c7cb6aaf04f7c578bf4d1a6e47dcd8b920c9a8d
6
+ metadata.gz: 1e1684de21ce2e210344d071901088eabe6a9063ce01930f2df37ae66248b71cc1680f00c512ba82844842e585d0d23b6e67b1c69452368fc26e5e20977aed27
7
+ data.tar.gz: 2a25adcffecc91d140912f3aef50d7fe6de776af814913935e24bc8351f1aa9bd7afa9bb582b34af9a53581d6d71e1510a9939233c22c8648fb919320fea68fa
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nvar (0.2.0)
4
+ nvar (0.3.0)
5
5
  activesupport (>= 5.0.0, < 8.0)
6
6
 
7
7
  GEM
@@ -148,6 +148,7 @@ GEM
148
148
  parser (3.3.0.5)
149
149
  ast (~> 2.4.1)
150
150
  racc
151
+ prism (0.24.0)
151
152
  psych (5.1.2)
152
153
  stringio
153
154
  racc (1.7.3)
@@ -225,6 +226,12 @@ GEM
225
226
  rubocop-performance (1.20.2)
226
227
  rubocop (>= 1.48.1, < 2.0)
227
228
  rubocop-ast (>= 1.30.0, < 2.0)
229
+ ruby-lsp (0.14.3)
230
+ language_server-protocol (~> 3.17.0)
231
+ prism (>= 0.22.0, < 0.25)
232
+ sorbet-runtime (>= 0.5.10782)
233
+ ruby-lsp-rspec (0.1.10)
234
+ ruby-lsp (~> 0.14.0)
228
235
  ruby-progressbar (1.13.0)
229
236
  ruby2_keywords (0.0.5)
230
237
  simplecov (0.22.0)
@@ -233,6 +240,7 @@ GEM
233
240
  simplecov_json_formatter (~> 0.1)
234
241
  simplecov-html (0.12.3)
235
242
  simplecov_json_formatter (0.1.4)
243
+ sorbet-runtime (0.5.11276)
236
244
  standard (1.34.0)
237
245
  language_server-protocol (~> 3.17.0.2)
238
246
  lint_roller (~> 1.0)
@@ -277,6 +285,7 @@ DEPENDENCIES
277
285
  rails
278
286
  rake (~> 12.0)
279
287
  rspec (~> 3.0)
288
+ ruby-lsp-rspec
280
289
  simplecov
281
290
  standardrb
282
291
  tempfile
data/README.md CHANGED
@@ -5,7 +5,17 @@ If your app relies on lots of environment secrets, onboarding's tough. New team
5
5
  You can use `Nvar` in Ruby apps, with out-of-the-box support provided for Rails.
6
6
  ## Installation
7
7
 
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
+
9
19
  ## Configuration
10
20
 
11
21
  `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.
@@ -36,7 +46,7 @@ This is just a glimpse of `Nvar`'s greater aim - centralizing configuration for
36
46
 
37
47
  ### Passthrough
38
48
 
39
- The final config option, `passthrough`, deserves some extra detail. By default, `Nvar` sets your environment constants to their actual values in development and production environments, and to their names in test environments.
49
+ The final config option, `passthrough`, deserves some extra detail. By default, `Nvar` sets your environment constants to their actual values in development and production environments, and to their names in test environments, in order to prevent your tests from depending on their values.
40
50
 
41
51
  In production/development, or in test with passthrough active:
42
52
 
@@ -52,17 +62,20 @@ irb(main):001:0> REQUIRED_ENV_VAR
52
62
  => "REQUIRED_ENV_VAR"
53
63
  ```
54
64
 
55
- Your tests shouldn't be reliant on your environment, so generally, you want to have `passthrough` set to `true` as little as possible. What it *is* useful for, however, is recording VCR cassettes. Set `passthrough: true` on necessary environment variables before recording VCR cassettes, then remove it and run your tests again to make sure they're not reliant on your environment.
65
+ Your tests shouldn't be reliant on your environment, so setting `passthrough` to `true` in `config/environment_variables.yml` isn't recommended. Passthrough is, however, useful for recording VCR cassettes. Set NVAR_PASSTHROUGH to a comma-separated list of environment variable names while running your tests, then unset it and run your tests again to make sure they're not reliant on your environment. For example:
56
66
 
67
+ ```
68
+ NVAR_PASSTHROUGH=GITHUB_APP_PRIVATE_KEY,GITHUB_APP_INSTALLATION_ID bundle exec rspec
69
+ ```
57
70
 
58
71
  ## Usage
59
72
 
60
73
  Now that you've been through and configured the environment variables that are necessary for your app, `Nvar` will write your environment variables to top-level constants, cast to any types you've specified, and raise an informative error if any are absent.
61
74
  ### .env files
62
75
 
63
- `Nvar` works well with gems like `dotenv-rails` that source their config from a `.env` file. If an environment variable is unset when the app initializes and isn't present in `.env`, it will be added to that file. If a default value is specified in your `Nvar` config, that will be passed to `.env` too.
76
+ `Nvar` works well with gems like `dotenv` that source their config from a `.env` file. If an environment variable is unset when the app initializes and isn't present in `.env`, it will be added to that file. If a default value is specified in your `Nvar` config, that will be passed to `.env` too.
64
77
 
65
- When using gems such as `dotenv-rails`, make sure you load those first so that the environment is ready for `Nvar` to check.
78
+ When using gems such as `dotenv`, make sure you load those first so that the environment is ready for `Nvar` to check.
66
79
 
67
80
  ### `rake nvar:verify_environment_file`
68
81
 
@@ -16,7 +16,7 @@ module Nvar
16
16
  @type = type
17
17
  @required = args[:required].nil? ? true : args[:required]
18
18
  @filter_from_requests = filter_from_requests.yield_self { |f| [true, false].include?(f) ? f : f&.to_sym }
19
- @value = fetch_value(**args.slice(:passthrough, :default_value))
19
+ @value = fetch_value(**args.slice(:passthrough, :default_value).with_defaults(passthrough: ENV.fetch("NVAR_PASSTHROUGH", "").split(",").include?(name.to_s)))
20
20
  @defined = true
21
21
  rescue KeyError
22
22
  @value = args[:default_value]
@@ -26,7 +26,7 @@ module Nvar
26
26
  def to_const
27
27
  raise Nvar::EnvironmentVariableNotPresentError, self unless defined
28
28
 
29
- Object.const_set(name, typecast_value)
29
+ Object.const_set(name, typecast_value) unless Object.const_defined?(name)
30
30
  end
31
31
 
32
32
  def set?
@@ -76,7 +76,7 @@ module Nvar
76
76
  end
77
77
 
78
78
  def fetch_value(passthrough: false, default_value: nil)
79
- return default_value || name if ENV["RAILS_ENV"] == "test" && !passthrough
79
+ return default_value || name.to_s if Nvar.env.test? && !passthrough
80
80
 
81
81
  required ? ENV.fetch(name.to_s) : ENV[name.to_s]
82
82
  end
data/lib/nvar/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nvar
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/nvar.rb CHANGED
@@ -4,11 +4,14 @@ require "nvar/version"
4
4
  require "nvar/environment_variable"
5
5
  require "nvar/engine" if defined?(Rails)
6
6
  require "active_support/core_ext/module/attribute_accessors"
7
+ require "active_support/core_ext/hash/reverse_merge"
8
+ require "active_support/string_inquirer"
7
9
 
8
10
  # Centralized configuration for required environment variables in your Ruby app.
9
11
  module Nvar
10
12
  mattr_accessor :config_file_path, default: File.expand_path("config/environment_variables.yml")
11
13
  mattr_accessor :env_file_path, default: File.expand_path(".env")
14
+ mattr_accessor :env
12
15
 
13
16
  # Comments in .env files must have a leading '#' symbol. This cannot be
14
17
  # followed by a space.
@@ -38,12 +41,21 @@ module Nvar
38
41
  end
39
42
 
40
43
  class << self
44
+ def env
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
50
+ end
51
+
41
52
  def configure_for_rails(app)
42
53
  self.config_file_path = app.root.join("config/environment_variables.yml")
43
54
  self.env_file_path = app.root.join(".env")
44
55
  [config_file_path, env_file_path].each do |path|
45
56
  File.open(path, "w") {} unless path.exist? # rubocop:disable Lint/EmptyBlock
46
57
  end
58
+ self.env = Rails.env
47
59
  end
48
60
 
49
61
  def load_all
@@ -62,7 +74,6 @@ module Nvar
62
74
 
63
75
  def all
64
76
  variables.map do |variable_name, config|
65
- # TODO: Passthrough from environment behaviour might need to go here?
66
77
  EnvironmentVariable.new(**(config || {}).merge(name: variable_name))
67
78
  end.partition(&:set?)
68
79
  end
data/nvar.gemspec CHANGED
@@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
33
33
  spec.add_development_dependency "byebug"
34
34
  spec.add_development_dependency "climate_control"
35
35
  spec.add_development_dependency "rails"
36
+ spec.add_development_dependency "ruby-lsp-rspec"
36
37
  spec.add_development_dependency "simplecov"
37
38
  spec.add_development_dependency "standardrb"
38
39
  spec.add_development_dependency "tempfile"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nvar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Fish
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-28 00:00:00.000000000 Z
11
+ date: 2024-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -86,6 +86,20 @@ dependencies:
86
86
  - - ">="
87
87
  - !ruby/object:Gem::Version
88
88
  version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: ruby-lsp-rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
89
103
  - !ruby/object:Gem::Dependency
90
104
  name: simplecov
91
105
  requirement: !ruby/object:Gem::Requirement
@@ -203,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
217
  - !ruby/object:Gem::Version
204
218
  version: '0'
205
219
  requirements: []
206
- rubygems_version: 3.5.5
220
+ rubygems_version: 3.5.6
207
221
  signing_key:
208
222
  specification_version: 4
209
223
  summary: Manage environment variables in Ruby