envp 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +31 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +44 -0
- data/LICENSE.txt +21 -0
- data/README.md +119 -0
- data/Rakefile +15 -0
- data/lib/envp/coercions/all.rb +16 -0
- data/lib/envp/coercions/array.rb +11 -0
- data/lib/envp/coercions/big_decimal.rb +12 -0
- data/lib/envp/coercions/bool.rb +14 -0
- data/lib/envp/coercions/date.rb +12 -0
- data/lib/envp/coercions/datetime.rb +12 -0
- data/lib/envp/coercions/float.rb +11 -0
- data/lib/envp/coercions/integer.rb +11 -0
- data/lib/envp/coercions/json.rb +12 -0
- data/lib/envp/coercions/pathname.rb +12 -0
- data/lib/envp/coercions/regexp.rb +11 -0
- data/lib/envp/coercions/registry.rb +32 -0
- data/lib/envp/coercions/string.rb +11 -0
- data/lib/envp/coercions/symbol.rb +11 -0
- data/lib/envp/coercions/time.rb +12 -0
- data/lib/envp/coercions/uri.rb +12 -0
- data/lib/envp/coercions.rb +36 -0
- data/lib/envp/errors.rb +12 -0
- data/lib/envp/parser.rb +81 -0
- data/lib/envp/result.rb +65 -0
- data/lib/envp/results.rb +39 -0
- data/lib/envp/vars/base.rb +26 -0
- data/lib/envp/vars/block.rb +22 -0
- data/lib/envp/vars/coerced.rb +34 -0
- data/lib/envp/vars/default_value.rb +26 -0
- data/lib/envp/vars/non_blank.rb +22 -0
- data/lib/envp/vars/required.rb +22 -0
- data/lib/envp/vars.rb +13 -0
- data/lib/envp/version.rb +5 -0
- data/lib/envp.rb +59 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9eadb093aedd1cb1d4720e048601211b6add41b9de3e1807f8467f1ef706b6fb
|
4
|
+
data.tar.gz: f836d9dcb8a009713ed99026b32a15bf0688b1f6329afb892aa35351a6b38501
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ea40c61d340de298b22ec9990348b0910f62eef4acd1f75c3c34d682859c38d88e5336aae246c384c4ee3e11598b33624e4d88f5e8c031149c8976a9a57519ce
|
7
|
+
data.tar.gz: 3e024daebc2dd670e05c2aa411fc981789f96f8cfd62df60a04f9848588d9b13990c28088719949fcd9682bd6182d0231ea6949543996a72b1c19b2817b8386c
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 3.2
|
3
|
+
SuggestExtensions: false
|
4
|
+
NewCops: enable
|
5
|
+
|
6
|
+
Metrics/CyclomaticComplexity:
|
7
|
+
Max: 9
|
8
|
+
|
9
|
+
Metrics/PerceivedComplexity:
|
10
|
+
Max: 9
|
11
|
+
|
12
|
+
Style/Documentation:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Style/StringLiterals:
|
16
|
+
Enabled: true
|
17
|
+
EnforcedStyle: double_quotes
|
18
|
+
|
19
|
+
Style/StringLiteralsInInterpolation:
|
20
|
+
Enabled: true
|
21
|
+
EnforcedStyle: double_quotes
|
22
|
+
|
23
|
+
Layout/LineLength:
|
24
|
+
Max: 120
|
25
|
+
|
26
|
+
Metrics/MethodLength:
|
27
|
+
Max: 12
|
28
|
+
|
29
|
+
Metrics/ClassLength:
|
30
|
+
Exclude:
|
31
|
+
- test/**/test_*.rb
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at arjan@arjanvandergaag.nl. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
envp (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
json (2.6.3)
|
11
|
+
minitest (5.18.0)
|
12
|
+
parallel (1.23.0)
|
13
|
+
parser (3.2.2.1)
|
14
|
+
ast (~> 2.4.1)
|
15
|
+
rainbow (3.1.1)
|
16
|
+
rake (13.0.6)
|
17
|
+
regexp_parser (2.8.0)
|
18
|
+
rexml (3.2.5)
|
19
|
+
rubocop (1.50.2)
|
20
|
+
json (~> 2.3)
|
21
|
+
parallel (~> 1.10)
|
22
|
+
parser (>= 3.2.0.0)
|
23
|
+
rainbow (>= 2.2.2, < 4.0)
|
24
|
+
regexp_parser (>= 1.8, < 3.0)
|
25
|
+
rexml (>= 3.2.5, < 4.0)
|
26
|
+
rubocop-ast (>= 1.28.0, < 2.0)
|
27
|
+
ruby-progressbar (~> 1.7)
|
28
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
29
|
+
rubocop-ast (1.28.1)
|
30
|
+
parser (>= 3.2.1.0)
|
31
|
+
ruby-progressbar (1.13.0)
|
32
|
+
unicode-display_width (2.4.2)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
arm64-darwin-22
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
envp!
|
39
|
+
minitest (~> 5.0)
|
40
|
+
rake (~> 13.0)
|
41
|
+
rubocop (~> 1.21)
|
42
|
+
|
43
|
+
BUNDLED WITH
|
44
|
+
2.4.10
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Arjan van der Gaag
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
# Envp
|
2
|
+
|
3
|
+
A Ruby gem for working with environment variables much like OptionParser does with command-line arguments.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add envp
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install envp
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
`Envp` allows you to write expectations for environment variables for your program.
|
18
|
+
|
19
|
+
### Simple example
|
20
|
+
|
21
|
+
Define a program in a new file `app.rb`:
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
reqiure 'envp'
|
25
|
+
|
26
|
+
pp Envp.parse do |e|
|
27
|
+
e.required("FOO")
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
Run the program without any special environment variables:
|
32
|
+
|
33
|
+
``` sh
|
34
|
+
$ ruby app.rb
|
35
|
+
app.rb:47:in `handle_errors': FOO: missing required value (Envp::Failure)
|
36
|
+
from app.rb:26:in `parse'
|
37
|
+
from -e:1:in `<main>'
|
38
|
+
```
|
39
|
+
|
40
|
+
Rather then let the program continue with missing required values, an exception is raised immediately.
|
41
|
+
|
42
|
+
Run the program with the required environment variables:
|
43
|
+
|
44
|
+
``` sh
|
45
|
+
$ FOO=bar ruby app.rb
|
46
|
+
{"FOO"=>"bar"}
|
47
|
+
```
|
48
|
+
|
49
|
+
The specified environment variables are parsed into a Ruby hash for use in your program.
|
50
|
+
|
51
|
+
### Coercions
|
52
|
+
|
53
|
+
Environment variables can be coerced from string values into richer types. For example, to parse a value into an integer:
|
54
|
+
|
55
|
+
``` ruby
|
56
|
+
reqiure 'envp'
|
57
|
+
|
58
|
+
pp Envp.parse do |e|
|
59
|
+
e.required("FOO", Integer)
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
Will result in:
|
64
|
+
|
65
|
+
``` sh
|
66
|
+
$ FOO=123 ruby app.rb
|
67
|
+
{"FOO"=>123}
|
68
|
+
```
|
69
|
+
|
70
|
+
Along with various predefined coercions, you can define your own coercions:
|
71
|
+
|
72
|
+
``` ruby
|
73
|
+
reqiure 'envp'
|
74
|
+
|
75
|
+
pp Envp.parse do |e|
|
76
|
+
e.accept(User) do |id|
|
77
|
+
User.find(id)
|
78
|
+
end
|
79
|
+
e.required("USER_ID", User)
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
### Full example
|
84
|
+
|
85
|
+
The following example demonstrates all features:
|
86
|
+
|
87
|
+
``` ruby
|
88
|
+
Envp.parse(constants: true, symbolize: true, normalize: true) do |e|
|
89
|
+
e.accept(User) do |id|
|
90
|
+
User.find(id)
|
91
|
+
end
|
92
|
+
e.required("USER_ID", User)
|
93
|
+
e.required("API_KEY", allow_blank: false) do |value|
|
94
|
+
DEFAULT_LOGGER.debug("using API_KEY #{value}")
|
95
|
+
end
|
96
|
+
e.optional("DEBUG", :bool, default: false)
|
97
|
+
end
|
98
|
+
# => {:user_id=>#<User...>, :api_key=>"abc123", :debug=>false}
|
99
|
+
Envp::DEBUG # => false
|
100
|
+
Envp::API_KEY # => "abc123"
|
101
|
+
```
|
102
|
+
|
103
|
+
## Development
|
104
|
+
|
105
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
106
|
+
|
107
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
108
|
+
|
109
|
+
## Contributing
|
110
|
+
|
111
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/avdgaag/envp. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/avdgaag/envp/blob/main/CODE_OF_CONDUCT.md).
|
112
|
+
|
113
|
+
## License
|
114
|
+
|
115
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
116
|
+
|
117
|
+
## Code of Conduct
|
118
|
+
|
119
|
+
Everyone interacting in the Envp project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/avdgaag/envp/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "minitest/test_task"
|
5
|
+
require "rubocop/rake_task"
|
6
|
+
|
7
|
+
Minitest::TestTask.create(:test) do |t|
|
8
|
+
t.libs << "test"
|
9
|
+
t.libs << "lib"
|
10
|
+
t.test_globs = FileList["test/**/test_*.rb"]
|
11
|
+
end
|
12
|
+
|
13
|
+
RuboCop::RakeTask.new
|
14
|
+
|
15
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "integer"
|
4
|
+
require_relative "float"
|
5
|
+
require_relative "big_decimal"
|
6
|
+
require_relative "pathname"
|
7
|
+
require_relative "time"
|
8
|
+
require_relative "datetime"
|
9
|
+
require_relative "date"
|
10
|
+
require_relative "uri"
|
11
|
+
require_relative "array"
|
12
|
+
require_relative "regexp"
|
13
|
+
require_relative "string"
|
14
|
+
require_relative "bool"
|
15
|
+
require_relative "json"
|
16
|
+
require_relative "symbol"
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "forwardable"
|
4
|
+
require_relative "../errors"
|
5
|
+
|
6
|
+
module Envp
|
7
|
+
module Coercions
|
8
|
+
class Registry
|
9
|
+
extend Forwardable
|
10
|
+
def_delegator :@coercions, :empty?
|
11
|
+
def_delegator :@coercions, :key?, :coerces?
|
12
|
+
|
13
|
+
def self.default
|
14
|
+
@default ||= new
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@coercions = {}
|
19
|
+
end
|
20
|
+
|
21
|
+
def register(target, &block)
|
22
|
+
@coercions[target] = block
|
23
|
+
end
|
24
|
+
|
25
|
+
def coerce(target, value)
|
26
|
+
raise UnknownCoercion unless coerces?(target)
|
27
|
+
|
28
|
+
@coercions.fetch(target).call(value)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Envp
|
4
|
+
# Coercions allow you to transform string values read from the environment and
|
5
|
+
# parse them into rich values. THere are various pre-defined coercions and you
|
6
|
+
# can define you own.
|
7
|
+
#
|
8
|
+
# ## Pre-defined coercions
|
9
|
+
#
|
10
|
+
# * `Array`
|
11
|
+
# * `BigDecimal`
|
12
|
+
# * `Date`
|
13
|
+
# * `DateTime`
|
14
|
+
# * `Time`
|
15
|
+
# * `Float`
|
16
|
+
# * `Integer`
|
17
|
+
# * `Pathname`
|
18
|
+
# * `Regexp`
|
19
|
+
# * `String`
|
20
|
+
# * `URI`
|
21
|
+
# * `:bool`
|
22
|
+
# * `JSON`
|
23
|
+
# * `Symbol`
|
24
|
+
#
|
25
|
+
# These coercions might require standard libraries. You can load exactly
|
26
|
+
# those you require or load all of them at once:
|
27
|
+
#
|
28
|
+
# require 'envp/coercions/array'
|
29
|
+
# require 'envp/coercions/all'
|
30
|
+
#
|
31
|
+
# ## Defining custom coercions
|
32
|
+
#
|
33
|
+
# See `Envp::Parser#accept`.
|
34
|
+
module Coercions
|
35
|
+
end
|
36
|
+
end
|
data/lib/envp/errors.rb
ADDED
data/lib/envp/parser.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "coercions/registry"
|
4
|
+
require_relative "results"
|
5
|
+
require_relative "vars"
|
6
|
+
require_relative "errors"
|
7
|
+
|
8
|
+
module Envp
|
9
|
+
# The `Parser` defines environment variable requirements along with custom
|
10
|
+
# available coercions, and then uses those requirements to read environment
|
11
|
+
# variables into a single result value.
|
12
|
+
class Parser
|
13
|
+
# @param vars [Set] initial set of vars.
|
14
|
+
# @param coercion_registry [ParserEnv::Coercions::Registry]
|
15
|
+
def initialize(vars = Set.new, coercion_registry = Coercions::Registry.default)
|
16
|
+
@vars = vars
|
17
|
+
@coercion_registry = coercion_registry
|
18
|
+
end
|
19
|
+
|
20
|
+
# Require an enviroment variable.
|
21
|
+
#
|
22
|
+
# @param name [String] the name of the environment variable to fetch.
|
23
|
+
# @param coercion [Object] what to coerce the fetched value to.
|
24
|
+
# @param allow_blank [Boolean] whether to fail when the value is empty (defaults to `true`)
|
25
|
+
# @yield value [Object] the coerced value fetched from the environment
|
26
|
+
# @return [void]
|
27
|
+
def required(name, coercion = nil, allow_blank: true, &block)
|
28
|
+
var = Vars::Base.new(name)
|
29
|
+
var = Vars::Required.new(var)
|
30
|
+
var = Vars::NonBlank.new(var) unless allow_blank
|
31
|
+
var = Vars::Coerced.new(var, coercion, @coercion_registry) if coercion
|
32
|
+
var = Vars::Block.new(var, &block) if block
|
33
|
+
@vars << var
|
34
|
+
end
|
35
|
+
|
36
|
+
# Get an enviroment variable, if it exists.
|
37
|
+
#
|
38
|
+
# @param name [String] the name of the environment variable to fetch.
|
39
|
+
# @param coercion [Object] what to coerce the fetched value to.
|
40
|
+
# @param default [Object] value to use when the value is not set (defaults to `nil`)
|
41
|
+
# @yield value [Object] the coerced value fetched from the environment
|
42
|
+
# @return [void]
|
43
|
+
def optional(name, coercion = nil, default: nil, &block)
|
44
|
+
var = Vars::Base.new(name)
|
45
|
+
var = Vars::DefaultValue.new(var, default)
|
46
|
+
var = Vars::Coerced.new(var, coercion, @coercion_registry) if coercion
|
47
|
+
var = Vars::Block.new(var, &block) if block
|
48
|
+
@vars << var
|
49
|
+
end
|
50
|
+
|
51
|
+
# Actually parse the specified variables from the given hash-like value
|
52
|
+
# (probably `ENV`).
|
53
|
+
#
|
54
|
+
# @param env [#to_h]
|
55
|
+
# @return [Envp::Results]
|
56
|
+
def parse(env)
|
57
|
+
@vars.each_with_object(Results.new) do |spec, r|
|
58
|
+
r[spec] = spec.from(env)
|
59
|
+
rescue StandardError => e
|
60
|
+
r[spec] = Result.error(e)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Define a custom coercion target. This can then be used in variable
|
65
|
+
# requirements as a coercion strategy.
|
66
|
+
#
|
67
|
+
# @example
|
68
|
+
# parser = Parser.new
|
69
|
+
# parser.accept(User) { |id| User.find(id) }
|
70
|
+
# parser.require("USER", User)
|
71
|
+
# parser.parse({ "USER" => "1" }) => { ok: { "USER" => user }}
|
72
|
+
# user # => #<User ...>
|
73
|
+
#
|
74
|
+
# @param target [Object]
|
75
|
+
# @yield value [String]
|
76
|
+
# @return [void]
|
77
|
+
def accept(target, &)
|
78
|
+
@coercion_registry.register(target, &)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/lib/envp/result.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Envp
|
4
|
+
# Work with special values representing successful and unsuccessful
|
5
|
+
# operations.
|
6
|
+
module Result
|
7
|
+
# Tag a value as being a failed result.
|
8
|
+
#
|
9
|
+
# @param value [Object]
|
10
|
+
# @return [Hash<Symbol, Object>]
|
11
|
+
def self.error(value)
|
12
|
+
{ error: value }
|
13
|
+
end
|
14
|
+
|
15
|
+
# Tag a value as being a succesful result.
|
16
|
+
#
|
17
|
+
# @param value [Object]
|
18
|
+
# @return [Hash<Symbol, Object>]
|
19
|
+
def self.ok(value)
|
20
|
+
{ ok: value }
|
21
|
+
end
|
22
|
+
|
23
|
+
# Apply a block to an ok-value, ignoring error-values.
|
24
|
+
#
|
25
|
+
# @param result [Hash<Symbol, Object>]
|
26
|
+
# @yield value [Object]
|
27
|
+
# @return [Hash<Symbol, Object>]
|
28
|
+
def self.map(result)
|
29
|
+
case result
|
30
|
+
in {ok: value}
|
31
|
+
ok(yield(value))
|
32
|
+
in {error: _}
|
33
|
+
result
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Apply a block to an error-value, ignoring ok-values.
|
38
|
+
#
|
39
|
+
# @param result [Hash<Symbol, Object>]
|
40
|
+
# @yield value [Object]
|
41
|
+
# @return [Hash<Symbol, Object>]
|
42
|
+
def self.map_error(result)
|
43
|
+
case result
|
44
|
+
in {ok: _}
|
45
|
+
result
|
46
|
+
in {error: err}
|
47
|
+
error(yield(err))
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Chain a block returning a result value to a result value.
|
52
|
+
#
|
53
|
+
# @param result [Hash<Symbol, Object>]
|
54
|
+
# @yield value [Object]
|
55
|
+
# @return [Hash<Symbol, Object>]
|
56
|
+
def self.then(result)
|
57
|
+
case result
|
58
|
+
in {ok: value}
|
59
|
+
yield(value)
|
60
|
+
in {error: _}
|
61
|
+
result
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/envp/results.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "forwardable"
|
4
|
+
require_relative "result"
|
5
|
+
|
6
|
+
module Envp
|
7
|
+
# A container for parsing results. Each known var is combined with a Result
|
8
|
+
# value. It then provides a single Result value representing either the
|
9
|
+
# successful parsings, or, if there are error values, all errors.
|
10
|
+
class Results
|
11
|
+
extend Forwardable
|
12
|
+
|
13
|
+
def_delegators :@vars, :empty?, :[], :[]=
|
14
|
+
|
15
|
+
# @param vars [Hash<Envp::Vars::Base, Hash<Symbol, Object>>]
|
16
|
+
def initialize(vars = {})
|
17
|
+
@vars = vars
|
18
|
+
end
|
19
|
+
|
20
|
+
# A single Result value for all ok vars or all error vars.
|
21
|
+
#
|
22
|
+
# @see Envp::Result
|
23
|
+
# @return [Hash<Symbol, Hash<String, Object>>]
|
24
|
+
def result
|
25
|
+
@vars.inject(Result.ok({})) do |acc, (key, value)|
|
26
|
+
case [acc, value]
|
27
|
+
in [{ok: _}, { error: error }]
|
28
|
+
Result.error(key.name => error)
|
29
|
+
in [{error: hash}, {error: error}]
|
30
|
+
Result.error(hash.merge(key.name => error))
|
31
|
+
in [{ok: hash}, {ok: value}]
|
32
|
+
Result.ok(hash.merge(key.name => value))
|
33
|
+
in [{error: _}, {ok: _}]
|
34
|
+
acc
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../result"
|
4
|
+
|
5
|
+
module Envp
|
6
|
+
module Vars
|
7
|
+
# A base specification for an environment variable to read.
|
8
|
+
class Base
|
9
|
+
# @return [String]
|
10
|
+
attr_reader :name
|
11
|
+
|
12
|
+
# @param name [String] name of the variable the read
|
13
|
+
def initialize(name)
|
14
|
+
@name = name
|
15
|
+
end
|
16
|
+
|
17
|
+
# Read the named variable from the given hash-like value.
|
18
|
+
#
|
19
|
+
# @param env [#to_h]
|
20
|
+
# @return [Hash<Symbol, Object>]
|
21
|
+
def from(env)
|
22
|
+
Result.ok(env.to_h[@name])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "delegate"
|
4
|
+
require_relative "base"
|
5
|
+
require_relative "../result"
|
6
|
+
|
7
|
+
module Envp
|
8
|
+
module Vars
|
9
|
+
# A `Var` that is read and than has a block applied to it, returning the
|
10
|
+
# block return value.
|
11
|
+
class Block < DelegateClass(Base)
|
12
|
+
def initialize(var, &block)
|
13
|
+
super(var)
|
14
|
+
@block = block
|
15
|
+
end
|
16
|
+
|
17
|
+
def from(env)
|
18
|
+
Result.map(__getobj__.from(env), &@block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "delegate"
|
4
|
+
require_relative "base"
|
5
|
+
require_relative "../result"
|
6
|
+
|
7
|
+
module Envp
|
8
|
+
module Vars
|
9
|
+
# A `Var` that is coerced using available transformations in the given
|
10
|
+
# coercions registry.
|
11
|
+
class Coerced < DelegateClass(Base)
|
12
|
+
# @param name [Base]
|
13
|
+
# @param target [Object]
|
14
|
+
# @param coercions [Envp::Coercions::Registry]
|
15
|
+
def initialize(var, target, coercions)
|
16
|
+
super(var)
|
17
|
+
@target = target
|
18
|
+
@coercions = coercions
|
19
|
+
end
|
20
|
+
|
21
|
+
def from(env)
|
22
|
+
Result.then(__getobj__.from(env)) do |value|
|
23
|
+
if @coercions.coerces?(@target)
|
24
|
+
Result.ok(@coercions.coerce(@target, value))
|
25
|
+
else
|
26
|
+
Result.error("unknown coercion #{@target.inspect}")
|
27
|
+
end
|
28
|
+
rescue StandardError => e
|
29
|
+
Result.error(e.message)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "delegate"
|
4
|
+
require_relative "base"
|
5
|
+
require_relative "../result"
|
6
|
+
|
7
|
+
module Envp
|
8
|
+
module Vars
|
9
|
+
# A `Var` that provides a default value when the value read from the
|
10
|
+
# environment is `nil`.
|
11
|
+
class DefaultValue < DelegateClass(Base)
|
12
|
+
# @param var [Base]
|
13
|
+
# @param default [Object] the default value to return.
|
14
|
+
def initialize(var, default)
|
15
|
+
super(var)
|
16
|
+
@default = default
|
17
|
+
end
|
18
|
+
|
19
|
+
def from(env)
|
20
|
+
Result.map(__getobj__.from(env)) do |value|
|
21
|
+
value || @default
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "delegate"
|
4
|
+
require_relative "base"
|
5
|
+
require_relative "../result"
|
6
|
+
|
7
|
+
module Envp
|
8
|
+
module Vars
|
9
|
+
# A `Var` that fails when the fetched value is empty.
|
10
|
+
class NonBlank < DelegateClass(Base)
|
11
|
+
def from(env)
|
12
|
+
Result.then(__getobj__.from(env)) do |value|
|
13
|
+
if value.nil? || value.strip.empty?
|
14
|
+
Result.error("disallowed blank value")
|
15
|
+
else
|
16
|
+
Result.ok(value)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "delegate"
|
4
|
+
require_relative "base"
|
5
|
+
require_relative "../result"
|
6
|
+
|
7
|
+
module Envp
|
8
|
+
module Vars
|
9
|
+
# A `Var` that fails when the fetched value is not present.
|
10
|
+
class Required < DelegateClass(Base)
|
11
|
+
def from(env)
|
12
|
+
Result.then(__getobj__.from(env)) do |value|
|
13
|
+
if value
|
14
|
+
Result.ok(value)
|
15
|
+
else
|
16
|
+
Result.error("missing required value")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/envp/vars.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "vars/base"
|
4
|
+
require_relative "vars/required"
|
5
|
+
require_relative "vars/non_blank"
|
6
|
+
require_relative "vars/default_value"
|
7
|
+
require_relative "vars/block"
|
8
|
+
require_relative "vars/coerced"
|
9
|
+
|
10
|
+
module Envp
|
11
|
+
module Vars
|
12
|
+
end
|
13
|
+
end
|
data/lib/envp/version.rb
ADDED
data/lib/envp.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "envp/version"
|
4
|
+
require_relative "envp/parser"
|
5
|
+
require_relative "envp/errors"
|
6
|
+
|
7
|
+
module Envp
|
8
|
+
# @param env [#to_h] (defaults to `ENV`)
|
9
|
+
# @param constants [Boolean, Object] whether to define constants for the
|
10
|
+
# loaded environment variables. When `true`, constants are defined on the
|
11
|
+
# `Envp` module. If any other object, that will be used to define
|
12
|
+
# constants in. Defaults to `false`.
|
13
|
+
# @param symbolize [Boolean] whether to transform loaded environment variables
|
14
|
+
# to symbols in the returned result. Defaults to `false`.
|
15
|
+
# @param normalize [Boolean] whether to rename loaded environment variables
|
16
|
+
# from UPPER_SNAKE_CASE to lower_snake_case. Defaults to `false`.
|
17
|
+
# @raise [Failure]
|
18
|
+
# @return [Hash]
|
19
|
+
def self.parse(env: ENV, constants: false, symbolize: false, normalize: false)
|
20
|
+
parser = Parser.new
|
21
|
+
yield parser
|
22
|
+
case parser.parse(env).result
|
23
|
+
in { ok: results }
|
24
|
+
handle_ok(results, constants, symbolize, normalize)
|
25
|
+
in { error: errors }
|
26
|
+
handle_errors(errors)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.handle_ok(results, constants, symbolize, normalize)
|
31
|
+
if constants == true
|
32
|
+
define_constants(Envp, results)
|
33
|
+
elsif constants.respond_to?(:const_set)
|
34
|
+
define_constants(constants, results)
|
35
|
+
elsif constants
|
36
|
+
raise "Cannot define new constants on #{constants.inspect}"
|
37
|
+
end
|
38
|
+
results = results.transform_keys(&:downcase) if normalize
|
39
|
+
results = results.transform_keys(&:to_sym) if symbolize
|
40
|
+
results
|
41
|
+
end
|
42
|
+
|
43
|
+
private_class_method :handle_ok
|
44
|
+
|
45
|
+
def self.handle_errors(errors)
|
46
|
+
msg = errors.map { |key, value| "#{key}: #{value}" }
|
47
|
+
raise Failure, msg.join("\n")
|
48
|
+
end
|
49
|
+
|
50
|
+
private_class_method :handle_errors
|
51
|
+
|
52
|
+
def self.define_constants(obj, results)
|
53
|
+
results.each do |key, value|
|
54
|
+
obj.const_set(key, value)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private_class_method :define_constants
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: envp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Arjan van der Gaag
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-05-09 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- arjan@arjanvandergaag.nl
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rubocop.yml"
|
21
|
+
- CHANGELOG.md
|
22
|
+
- CODE_OF_CONDUCT.md
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/envp.rb
|
29
|
+
- lib/envp/coercions.rb
|
30
|
+
- lib/envp/coercions/all.rb
|
31
|
+
- lib/envp/coercions/array.rb
|
32
|
+
- lib/envp/coercions/big_decimal.rb
|
33
|
+
- lib/envp/coercions/bool.rb
|
34
|
+
- lib/envp/coercions/date.rb
|
35
|
+
- lib/envp/coercions/datetime.rb
|
36
|
+
- lib/envp/coercions/float.rb
|
37
|
+
- lib/envp/coercions/integer.rb
|
38
|
+
- lib/envp/coercions/json.rb
|
39
|
+
- lib/envp/coercions/pathname.rb
|
40
|
+
- lib/envp/coercions/regexp.rb
|
41
|
+
- lib/envp/coercions/registry.rb
|
42
|
+
- lib/envp/coercions/string.rb
|
43
|
+
- lib/envp/coercions/symbol.rb
|
44
|
+
- lib/envp/coercions/time.rb
|
45
|
+
- lib/envp/coercions/uri.rb
|
46
|
+
- lib/envp/errors.rb
|
47
|
+
- lib/envp/parser.rb
|
48
|
+
- lib/envp/result.rb
|
49
|
+
- lib/envp/results.rb
|
50
|
+
- lib/envp/vars.rb
|
51
|
+
- lib/envp/vars/base.rb
|
52
|
+
- lib/envp/vars/block.rb
|
53
|
+
- lib/envp/vars/coerced.rb
|
54
|
+
- lib/envp/vars/default_value.rb
|
55
|
+
- lib/envp/vars/non_blank.rb
|
56
|
+
- lib/envp/vars/required.rb
|
57
|
+
- lib/envp/version.rb
|
58
|
+
homepage: https://github.com/avdgaag/envp
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata:
|
62
|
+
homepage_uri: https://github.com/avdgaag/envp
|
63
|
+
source_code_uri: https://github.com/avdgaag/envp
|
64
|
+
rubygems_mfa_required: 'true'
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 3.2.0
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubygems_version: 3.4.10
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Work with environment variables effectively.
|
84
|
+
test_files: []
|