env_vars 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +15 -0
- data/.travis.yml +7 -5
- data/Gemfile +3 -1
- data/README.md +39 -13
- data/Rakefile +6 -1
- data/env_vars.gemspec +8 -3
- data/lib/env_vars.rb +44 -19
- data/lib/env_vars/dotenv.rb +2 -0
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e32d97f4e650d16b8c82ec8dd85bc19112788797262c00425e83a1d8594678f3
|
4
|
+
data.tar.gz: ebec12747a93d37dbeadf026ec1140dbcb1122eca498db9db7f5612221868cc4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6822500b56e48d42f508d128e505b288133724996a926dab44ecb6e43b8373ee6ea177faa6b30a21e48a3e2e2f9eb980d950140f1beca0447d6475aa1ce48ea
|
7
|
+
data.tar.gz: b4240b420772985e2c3403a7a5abd047b0b8bc8dfc0500340ef3a1e187dea6e181df0bbb4cea52652603c3bc04046b2cde98e85dda48d888067c8305070f1370
|
data/.rubocop.yml
ADDED
data/.travis.yml
CHANGED
@@ -4,13 +4,15 @@ sudo: false
|
|
4
4
|
notifications:
|
5
5
|
email: false
|
6
6
|
rvm:
|
7
|
-
- 2.
|
7
|
+
- 2.7.1
|
8
8
|
before_script:
|
9
|
-
- curl -L
|
10
|
-
-
|
11
|
-
|
9
|
+
- "curl -L
|
10
|
+
https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64
|
11
|
+
> ./cc-test-reporter"
|
12
|
+
- chmod +x ./cc-test-reporter
|
13
|
+
- "./cc-test-reporter before-build"
|
12
14
|
after_script:
|
13
|
-
- "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
|
15
|
+
- "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
|
14
16
|
env:
|
15
17
|
global:
|
16
18
|
secure: dXC3R4mvHkeXyhKskgf5/3/LUtBYKpDcTpNGbuOpnoXAaB87fHgjhDtwSazYERE5i0FYeMHWBWKUosQZkCOCydXCuZQ7Q1AnqjnouiCoaj675uvs/KamRAoknQVzxbSk5nxSiqth2fMDu6DTm8ZcbAXkCJj3JwNe0x/zZrx6ZfqEj928NdL2qxTbGLciFyovjhjwIx5ZNKJ+LlvwZ5uGdaGJvN1K7xuATcrRFyyPRzd0oPGFJb7pBwbkK0VxmdTMKcCnXmovoIIZPsXCMW1zvfE30McrCOyHrE/4OQWFI0FG7QJAzM3ekioWQj94JRkno3NPflTWBqn+tSQmjgKkSuooJQgbJ0eWAD0uU2JBebjrXx4/AvWlUiB4mG4u7OGUbq8TbY2EHhqb5JtTuAo3qy/D5xUtZaSISD70sQqfE6ZdjVlBaC9hDSATYXNyiUYGihslEUySmCqH94ZBkm1c5CKDPm1bnBs8eyzI2NqO6EA/yf8/U97fGKI1EmjNQm888MIgbgSeKlWfc6EonWMxlaZ8SG/J4cFx43nlUPPmP/q488wKz8qaCeVpvuX29mN+8LQmQT0zQlVNZIHfI64tIcdE8kvK/vu+9iq7UlPim2VPNf8B9aHX67NUli0FVo2acvCgivIcZ75YJFSCmy8IhNX/XaJD5AdQgVmwP0A/uk0=
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -39,7 +39,8 @@ Config.timeout
|
|
39
39
|
Config.force_ssl?
|
40
40
|
```
|
41
41
|
|
42
|
-
You can specify the description for both `mandatory` and `optional` methods;
|
42
|
+
You can specify the description for both `mandatory` and `optional` methods;
|
43
|
+
this will be used in exceptions.
|
43
44
|
|
44
45
|
```ruby
|
45
46
|
Config = Env::Vars.new do
|
@@ -49,7 +50,8 @@ end
|
|
49
50
|
#=> Env::Vars::MissingEnvironmentVariable: MISSING_VAR (this is important) is not defined
|
50
51
|
```
|
51
52
|
|
52
|
-
If you're going to use `env_vars` as your main configuration object, you can
|
53
|
+
If you're going to use `env_vars` as your main configuration object, you can
|
54
|
+
also set arbitrary properties, like the following:
|
53
55
|
|
54
56
|
```ruby
|
55
57
|
Config = Env::Vars.new do
|
@@ -63,7 +65,8 @@ Config.redis.get("key")
|
|
63
65
|
#=> "value"
|
64
66
|
```
|
65
67
|
|
66
|
-
Values are cached by default. If you want to dynamically generate new values,
|
68
|
+
Values are cached by default. If you want to dynamically generate new values,
|
69
|
+
set `cache: false`.
|
67
70
|
|
68
71
|
```ruby
|
69
72
|
Config = Env::Vars.new do
|
@@ -71,7 +74,10 @@ Config = Env::Vars.new do
|
|
71
74
|
end
|
72
75
|
```
|
73
76
|
|
74
|
-
You may want to start a debug session without raising exceptions for missing
|
77
|
+
You may want to start a debug session without raising exceptions for missing
|
78
|
+
variables. In this case, just pass `raise_exception: false` instead to log error
|
79
|
+
messages to `$stderr`. This is especially great with Rails' credentials command
|
80
|
+
(`rails credentials:edit`) when already defined the configuration.
|
75
81
|
|
76
82
|
```ruby
|
77
83
|
Config = Env::Vars.new(raise_exception: false) do
|
@@ -89,13 +95,20 @@ You can coerce values to the following types:
|
|
89
95
|
- `int`: E.g. `optional :timeout, int`.
|
90
96
|
- `float`: E.g. `optional :wait, float`.
|
91
97
|
- `bigdecimal`: E.g. `optional :fee, bigdecimal`.
|
92
|
-
- `bool`: E.g. `optional :force_ssl, bool`. Any of `yes`, `true` or `1` is
|
98
|
+
- `bool`: E.g. `optional :force_ssl, bool`. Any of `yes`, `true` or `1` is
|
99
|
+
considered as `true`. Any other value will be coerced to `false`.
|
93
100
|
- `symbol`: E.g. `optional :app_name, symbol`.
|
94
|
-
- `array`: E.g. `optional :chars, array` or `optional :numbers, array(int)`. The
|
101
|
+
- `array`: E.g. `optional :chars, array` or `optional :numbers, array(int)`. The
|
102
|
+
environment variable must be something like `a,b,c`.
|
103
|
+
- `json`: E.g. `mandatory :keyring, json`. The environment variable must be
|
104
|
+
parseable by `JSON.parse(content)`.
|
95
105
|
|
96
106
|
### Dotenv integration
|
97
107
|
|
98
|
-
If you're using [dotenv](https://rubygems.org/gems/dotenv), you can simply
|
108
|
+
If you're using [dotenv](https://rubygems.org/gems/dotenv), you can simply
|
109
|
+
require `env_vars/dotenv`. This will load environment variables from
|
110
|
+
`.env.local.%{environment}`, `.env.local`, `.env.%{environment}` and `.env`
|
111
|
+
files, respectively. You _must_ add `dotenv` to your `Gemfile`.
|
99
112
|
|
100
113
|
```ruby
|
101
114
|
require "env_vars/dotenv"
|
@@ -103,7 +116,9 @@ require "env_vars/dotenv"
|
|
103
116
|
|
104
117
|
### Configuring Rails
|
105
118
|
|
106
|
-
If you want to use `env_vars` even on your Rails configuration files like
|
119
|
+
If you want to use `env_vars` even on your Rails configuration files like
|
120
|
+
`database.yml` and `secrets.yml`, you must load it from `config/boot.rb`, right
|
121
|
+
after setting up Bundler.
|
107
122
|
|
108
123
|
```ruby
|
109
124
|
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __FILE__)
|
@@ -118,18 +133,29 @@ require File.expand_path("../config", __FILE__)
|
|
118
133
|
|
119
134
|
## Development
|
120
135
|
|
121
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
136
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
137
|
+
`rake test` to run the tests. You can also run `bin/console` for an interactive
|
138
|
+
prompt that will allow you to experiment.
|
122
139
|
|
123
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To
|
140
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
141
|
+
release a new version, update the version number in `version.rb`, and then run
|
142
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
143
|
+
git commits and tags, and push the `.gem` file to
|
144
|
+
[rubygems.org](https://rubygems.org).
|
124
145
|
|
125
146
|
## Contributing
|
126
147
|
|
127
|
-
Bug reports and pull requests are welcome on GitHub at
|
148
|
+
Bug reports and pull requests are welcome on GitHub at
|
149
|
+
https://github.com/fnando/env_vars. This project is intended to be a safe,
|
150
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
151
|
+
the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
128
152
|
|
129
153
|
## License
|
130
154
|
|
131
|
-
The gem is available as open source under the terms of the
|
155
|
+
The gem is available as open source under the terms of the
|
156
|
+
[MIT License](http://opensource.org/licenses/MIT).
|
132
157
|
|
133
158
|
## Icon
|
134
159
|
|
135
|
-
Icon made by [eucalyp](https://www.flaticon.com/authors/eucalyp) from
|
160
|
+
Icon made by [eucalyp](https://www.flaticon.com/authors/eucalyp) from
|
161
|
+
[Flaticon](https://www.flaticon.com/) is licensed by Creative Commons BY 3.0.
|
data/Rakefile
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "bundler/gem_tasks"
|
2
4
|
require "rake/testtask"
|
5
|
+
require "rubocop/rake_task"
|
3
6
|
|
4
7
|
Rake::TestTask.new(:test) do |t|
|
5
8
|
t.libs << "test"
|
@@ -7,4 +10,6 @@ Rake::TestTask.new(:test) do |t|
|
|
7
10
|
t.test_files = FileList["test/**/*_test.rb"]
|
8
11
|
end
|
9
12
|
|
10
|
-
|
13
|
+
RuboCop::RakeTask.new
|
14
|
+
|
15
|
+
task default: %i[test rubocop]
|
data/env_vars.gemspec
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./lib/env_vars"
|
2
4
|
|
3
5
|
Gem::Specification.new do |spec|
|
4
6
|
spec.name = "env_vars"
|
@@ -6,12 +8,15 @@ Gem::Specification.new do |spec|
|
|
6
8
|
spec.authors = ["Nando Vieira"]
|
7
9
|
spec.email = ["fnando.vieira@gmail.com"]
|
8
10
|
|
9
|
-
spec.summary = "Access environment variables. Also includes presence
|
11
|
+
spec.summary = "Access environment variables. Also includes presence " \
|
12
|
+
"validation, type coercion and default values."
|
10
13
|
spec.description = spec.summary
|
11
14
|
spec.homepage = "https://github.com/fnando/env_vars"
|
12
15
|
spec.license = "MIT"
|
13
16
|
|
14
|
-
spec.files = `git ls-files -z
|
17
|
+
spec.files = `git ls-files -z`
|
18
|
+
.split("\x0")
|
19
|
+
.reject {|f| f.match(%r{^(test|spec|features)/}) }
|
15
20
|
spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
|
16
21
|
spec.require_paths = ["lib"]
|
17
22
|
|
data/lib/env_vars.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Env
|
2
4
|
class Vars
|
3
|
-
VERSION = "1.
|
4
|
-
BOOL_TRUE = ["yes", "true", "1", true]
|
5
|
-
BOOL_FALSE = [
|
5
|
+
VERSION = "1.1.0"
|
6
|
+
BOOL_TRUE = ["yes", "true", "1", true].freeze
|
7
|
+
BOOL_FALSE = %w[no false].freeze
|
6
8
|
|
7
9
|
MissingEnvironmentVariable = Class.new(StandardError)
|
8
10
|
MissingCallable = Class.new(StandardError)
|
@@ -18,9 +20,16 @@ module Env
|
|
18
20
|
def to_s
|
19
21
|
"#<Env::Vars>"
|
20
22
|
end
|
21
|
-
|
23
|
+
alias inspect to_s
|
22
24
|
|
23
|
-
def set(
|
25
|
+
def set(
|
26
|
+
name,
|
27
|
+
type,
|
28
|
+
default = nil,
|
29
|
+
required: false,
|
30
|
+
aliases: [],
|
31
|
+
description: nil
|
32
|
+
)
|
24
33
|
name = name.to_s
|
25
34
|
env_var = name.upcase
|
26
35
|
name = "#{name}?" if type == bool
|
@@ -29,6 +38,7 @@ module Env
|
|
29
38
|
|
30
39
|
define_singleton_method(name) do
|
31
40
|
return default unless @env.key?(env_var)
|
41
|
+
|
32
42
|
coerce(type, @env[env_var])
|
33
43
|
end
|
34
44
|
|
@@ -53,16 +63,25 @@ module Env
|
|
53
63
|
end
|
54
64
|
|
55
65
|
def mandatory(name, type, aliases: [], description: nil)
|
56
|
-
set(
|
66
|
+
set(
|
67
|
+
name,
|
68
|
+
type,
|
69
|
+
required: true,
|
70
|
+
aliases: aliases,
|
71
|
+
description: description
|
72
|
+
)
|
57
73
|
end
|
58
74
|
|
59
75
|
def optional(name, type, default = nil, aliases: [], description: nil)
|
60
76
|
set(name, type, default, aliases: aliases, description: description)
|
61
77
|
end
|
62
78
|
|
63
|
-
def property(name, func = nil, cache: true, description: nil, &block)
|
79
|
+
def property(name, func = nil, cache: true, description: nil, &block) # rubocop:disable Lint/UnusedMethodArgument
|
64
80
|
callable = (func || block)
|
65
|
-
|
81
|
+
|
82
|
+
unless callable
|
83
|
+
raise MissingCallable, "arg[1] must respond to #call or pass a block"
|
84
|
+
end
|
66
85
|
|
67
86
|
if cache
|
68
87
|
define_singleton_method(name) do
|
@@ -102,37 +121,43 @@ module Env
|
|
102
121
|
[:array, type]
|
103
122
|
end
|
104
123
|
|
105
|
-
|
124
|
+
def json
|
125
|
+
:json
|
126
|
+
end
|
106
127
|
|
107
|
-
def coerce_to_string(value)
|
128
|
+
private def coerce_to_string(value)
|
108
129
|
value
|
109
130
|
end
|
110
131
|
|
111
|
-
def coerce_to_bool(value)
|
132
|
+
private def coerce_to_bool(value)
|
112
133
|
BOOL_TRUE.include?(value)
|
113
134
|
end
|
114
135
|
|
115
|
-
def coerce_to_int(value)
|
136
|
+
private def coerce_to_int(value)
|
116
137
|
Integer(value) if !BOOL_FALSE.include?(value) && value
|
117
138
|
end
|
118
139
|
|
119
|
-
def coerce_to_float(value)
|
140
|
+
private def coerce_to_float(value)
|
120
141
|
Float(value) if value
|
121
142
|
end
|
122
143
|
|
123
|
-
def coerce_to_bigdecimal(value)
|
144
|
+
private def coerce_to_bigdecimal(value)
|
124
145
|
BigDecimal(value) if value
|
125
146
|
end
|
126
147
|
|
127
|
-
def coerce_to_symbol(value)
|
128
|
-
value
|
148
|
+
private def coerce_to_symbol(value)
|
149
|
+
value&.to_sym
|
150
|
+
end
|
151
|
+
|
152
|
+
private def coerce_to_array(value, type)
|
153
|
+
value&.split(/, */)&.map {|v| coerce(type, v) }
|
129
154
|
end
|
130
155
|
|
131
|
-
def
|
132
|
-
value &&
|
156
|
+
private def coerce_to_json(value)
|
157
|
+
value && JSON.parse(value)
|
133
158
|
end
|
134
159
|
|
135
|
-
def coerce(type, value)
|
160
|
+
private def coerce(type, value)
|
136
161
|
main_type, sub_type = type
|
137
162
|
args = [value]
|
138
163
|
args << sub_type if sub_type
|
data/lib/env_vars/dotenv.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: env_vars
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -103,6 +103,7 @@ extensions: []
|
|
103
103
|
extra_rdoc_files: []
|
104
104
|
files:
|
105
105
|
- ".gitignore"
|
106
|
+
- ".rubocop.yml"
|
106
107
|
- ".travis.yml"
|
107
108
|
- CODE_OF_CONDUCT.md
|
108
109
|
- Gemfile
|
@@ -118,7 +119,7 @@ homepage: https://github.com/fnando/env_vars
|
|
118
119
|
licenses:
|
119
120
|
- MIT
|
120
121
|
metadata: {}
|
121
|
-
post_install_message:
|
122
|
+
post_install_message:
|
122
123
|
rdoc_options: []
|
123
124
|
require_paths:
|
124
125
|
- lib
|
@@ -133,9 +134,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
134
|
- !ruby/object:Gem::Version
|
134
135
|
version: '0'
|
135
136
|
requirements: []
|
136
|
-
|
137
|
-
|
138
|
-
signing_key:
|
137
|
+
rubygems_version: 3.1.2
|
138
|
+
signing_key:
|
139
139
|
specification_version: 4
|
140
140
|
summary: Access environment variables. Also includes presence validation, type coercion
|
141
141
|
and default values.
|