env_vars 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +12 -1
- data/README.md +29 -3
- data/env_vars.gemspec +1 -0
- data/lib/env_vars.rb +66 -16
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea60318c5b3551b75421b50696aeda6d9fe2262d
|
4
|
+
data.tar.gz: 58a80b5e1c90243a78921512ce6f1b6b579dc459
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd932edac1256b348f2575243632971f496e07e561a3e5edfc52180aa118c770357e052c556bc4bb2d84a8a148825435bf514a367689debdadbee8b241046e19
|
7
|
+
data.tar.gz: b04854b098b359b9a38a1e621b38d3d05ebe814a65cd0621abee30c41ce70099d62cb22e581ab25fb900568c83773eafa76b556123e06260b4682e3b6c910bab
|
data/.travis.yml
CHANGED
@@ -4,5 +4,16 @@ sudo: false
|
|
4
4
|
notifications:
|
5
5
|
email: false
|
6
6
|
rvm:
|
7
|
-
|
7
|
+
- 2.4.2
|
8
|
+
- 2.3.5
|
9
|
+
- 2.2.8
|
8
10
|
before_install: gem install bundler
|
11
|
+
before_script:
|
12
|
+
- "curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter"
|
13
|
+
- "chmod +x ./cc-test-reporter"
|
14
|
+
- "./cc-test-reporter before-build"
|
15
|
+
after_script:
|
16
|
+
- "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
|
17
|
+
env:
|
18
|
+
global:
|
19
|
+
secure: hRsybxCL3vUEhYtEdUIJ6pPyFaeVH/Tc1crVXJ0JCmem/rSv/PHS0jUzb8R4EiQKdwFFDgEciASW71XwpesIoY+KbUPfjvG9M8NnKscSuTpX/9CAVvoEoLzUtHYV5VNx6St2YTb1nStXhvGqBN0y5VNil9t5q9NIEM65eSgZuO7lvzAGgvUHYOkmljHqHb2rXejdAJ5k3NVbKPU3fByGGxvO9SiJ4RLWWpNhCwCWjVlds5+wffiHO2ojWdfLYikaIIcuqZzcyYuKUch16guPfz8+AsYpqHTsf+s81UnNKLk3Ut9zl5JoxCkkTSt1ZzAHNEceR4KXgRYxFZh3uHg8fXrYqdc4A4tgHqsndXeZnEFFnmAlOvjbGldtYoSjzJGBaSxKei9mZbeFy6ytlwZeMp22hBwflFUlbxF/O8IpvSRAq1M4QG1nfEfKCXhyFCmOaeS1BcgcFLzXiDYibCnAfLmK/EMa5V+zo4USjiRmzSBmQFdNk91x+zOGRQOz6lt3s6OEm98FHh/J9Y4tQ9DkkhwS6x9MuSfdjEQkyhvB2xsl9SOfdlU5Ork+FBkRFGlYtiAcHwVa6e0ikJCAGFW2xVqKwq9dT/Wvg4VNtkCmJORdsuxZf/Jtgh2k5gjFi7ZO4mViIxMjIAQqUA/+XR9dFxuAfSZC6FJG4l4ixqvmNoM=
|
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# Env::Vars
|
2
2
|
|
3
|
-
[](https://travis-ci.org/fnando/env_vars)
|
4
|
+
[](https://codeclimate.com/github/fnando/env_vars)
|
5
|
+
[](https://codeclimate.com/github/fnando/env_vars/coverage)
|
6
|
+
[](https://rubygems.org/gems/env_vars)
|
7
|
+
[](https://rubygems.org/gems/env_vars)
|
4
8
|
|
5
9
|
Access environment variables. Also includes presence validation, type coercion and default values.
|
6
10
|
|
@@ -40,7 +44,8 @@ If you're going to use `env_vars` as your main configuration object, you can als
|
|
40
44
|
```ruby
|
41
45
|
Config = Env::Vars.new do
|
42
46
|
optional :redis_url, string, "redis://127.0.0.1"
|
43
|
-
property :redis, -> { Redis.new }
|
47
|
+
property :redis, -> { Redis.new } # pass an object that responds to #call
|
48
|
+
property(:now) { Time.now } # or pass a block.
|
44
49
|
end
|
45
50
|
|
46
51
|
Config.redis.set("key", "value")
|
@@ -48,6 +53,27 @@ Config.redis.get("key")
|
|
48
53
|
#=> "value"
|
49
54
|
```
|
50
55
|
|
56
|
+
Values are cached by default. If you want to dynamically generate new values, set `cache: false`.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
Config = Env::Vars.new do
|
60
|
+
property(:uuid, cache: false) { SecureRandom.uuid }
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
### Types
|
65
|
+
|
66
|
+
You can coerce values to the following types:
|
67
|
+
|
68
|
+
- `string`: Is the default. E.g. `optional :name, string`.
|
69
|
+
- `int`: E.g. `optional :timeout, int`.
|
70
|
+
- `float`: E.g. `optional :wait, float`.
|
71
|
+
- `bool`: E.g. `optional :force_ssl, bool`. Any of `yes`, `true` or `1` is considered as `true`. Any other value will be coerced to `false`.
|
72
|
+
- `symbol`: E.g. `optional :app_name, symbol`.
|
73
|
+
- `array`: E.g. `optional :chars, array` or `optional :numbers, array(int)`. The environment variable must be something like `a,b,c`.
|
74
|
+
|
75
|
+
### Dotenv integration
|
76
|
+
|
51
77
|
If you're using [dotenv](https://rubygems.org/gems/dotenv), you can simply require `env_vars/dotenv`. This will load environment variables from `.env.local.%{environment}`, `.env.local`, `.env.%{environment}` and `.env` files, respectively. You _must_ add `dotenv` to your `Gemfile`.
|
52
78
|
|
53
79
|
```ruby
|
@@ -77,7 +103,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
77
103
|
|
78
104
|
## Contributing
|
79
105
|
|
80
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/fnando/env_vars. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
106
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fnando/env_vars. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
81
107
|
|
82
108
|
|
83
109
|
## License
|
data/env_vars.gemspec
CHANGED
data/lib/env_vars.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Env
|
2
4
|
class Vars
|
3
|
-
VERSION = "0.
|
5
|
+
VERSION = "0.5.0"
|
4
6
|
BOOL_TRUE = ["yes", "true", "1", true]
|
5
7
|
BOOL_FALSE = ["no", "false"]
|
6
8
|
|
7
9
|
MissingEnvironmentVariable = Class.new(StandardError)
|
10
|
+
MissingCallable = Class.new(StandardError)
|
8
11
|
|
9
|
-
def initialize(&block)
|
12
|
+
def initialize(env = ENV, &block)
|
13
|
+
@env = env
|
10
14
|
instance_eval(&block)
|
11
15
|
end
|
12
16
|
|
@@ -18,16 +22,8 @@ module Env
|
|
18
22
|
validate!(env_var, required)
|
19
23
|
|
20
24
|
define_singleton_method(name) do
|
21
|
-
|
22
|
-
|
23
|
-
case type
|
24
|
-
when bool
|
25
|
-
BOOL_TRUE.include?(value)
|
26
|
-
when int
|
27
|
-
Integer(value) if !BOOL_FALSE.include?(value) && value
|
28
|
-
else
|
29
|
-
value
|
30
|
-
end
|
25
|
+
return default unless @env.key?(env_var)
|
26
|
+
coerce(type, @env[env_var])
|
31
27
|
end
|
32
28
|
|
33
29
|
aliases.each do |alias_name|
|
@@ -36,7 +32,8 @@ module Env
|
|
36
32
|
end
|
37
33
|
|
38
34
|
def validate!(env_var, required)
|
39
|
-
|
35
|
+
return unless required
|
36
|
+
raise MissingEnvironmentVariable, "#{env_var} is not defined" unless @env.key?(env_var)
|
40
37
|
end
|
41
38
|
|
42
39
|
def mandatory(name, type, aliases: [])
|
@@ -47,9 +44,16 @@ module Env
|
|
47
44
|
set(name, type, default, aliases: aliases)
|
48
45
|
end
|
49
46
|
|
50
|
-
def property(name, func)
|
51
|
-
|
52
|
-
|
47
|
+
def property(name, func = nil, cache: true, &block)
|
48
|
+
callable = (func || block)
|
49
|
+
raise MissingCallable, "arg[1] must respond to #call or pass a block" unless callable
|
50
|
+
|
51
|
+
if cache
|
52
|
+
value = callable.call
|
53
|
+
define_singleton_method(name) { value }
|
54
|
+
else
|
55
|
+
define_singleton_method(name) { callable.call }
|
56
|
+
end
|
53
57
|
end
|
54
58
|
|
55
59
|
def int
|
@@ -63,5 +67,51 @@ module Env
|
|
63
67
|
def bool
|
64
68
|
:bool
|
65
69
|
end
|
70
|
+
|
71
|
+
def symbol
|
72
|
+
:symbol
|
73
|
+
end
|
74
|
+
|
75
|
+
def float
|
76
|
+
:float
|
77
|
+
end
|
78
|
+
|
79
|
+
def array(type = string)
|
80
|
+
[:array, type]
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def coerce_to_string(value)
|
86
|
+
value
|
87
|
+
end
|
88
|
+
|
89
|
+
def coerce_to_bool(value)
|
90
|
+
BOOL_TRUE.include?(value)
|
91
|
+
end
|
92
|
+
|
93
|
+
def coerce_to_int(value)
|
94
|
+
Integer(value) if !BOOL_FALSE.include?(value) && value
|
95
|
+
end
|
96
|
+
|
97
|
+
def coerce_to_float(value)
|
98
|
+
Float(value) if value
|
99
|
+
end
|
100
|
+
|
101
|
+
def coerce_to_symbol(value)
|
102
|
+
value && value.to_sym
|
103
|
+
end
|
104
|
+
|
105
|
+
def coerce_to_array(value, type)
|
106
|
+
value && value.split(/, */).map {|v| coerce(type, v) }
|
107
|
+
end
|
108
|
+
|
109
|
+
def coerce(type, value)
|
110
|
+
main_type, sub_type = type
|
111
|
+
args = [value]
|
112
|
+
args << sub_type if sub_type
|
113
|
+
|
114
|
+
send("coerce_to_#{main_type}", *args)
|
115
|
+
end
|
66
116
|
end
|
67
117
|
end
|
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: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: Access environment variables. Also includes presence validation, type
|
70
84
|
coercion and default values.
|
71
85
|
email:
|
@@ -104,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
118
|
version: '0'
|
105
119
|
requirements: []
|
106
120
|
rubyforge_project:
|
107
|
-
rubygems_version: 2.
|
121
|
+
rubygems_version: 2.6.13
|
108
122
|
signing_key:
|
109
123
|
specification_version: 4
|
110
124
|
summary: Access environment variables. Also includes presence validation, type coercion
|