philiprehberger-env_loader 0.3.0 → 0.4.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/CHANGELOG.md +6 -0
- data/README.md +18 -0
- data/lib/philiprehberger/env_loader/version.rb +1 -1
- data/lib/philiprehberger/env_loader.rb +43 -2
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: eb02740e5ccfd37833abf860aab8ab0c2f8a46cab63f1b1bcd17746384212c3a
|
|
4
|
+
data.tar.gz: 8641f1e44aa514fa8dfc5d1ca2643431ff0e48de19bc4ea0825ef10f2bde506e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3e4a088a9664ff7325464b2eac8330178e7bb7a83a91869e9938c80195597aa668676b306cf850e62de2250e05c09b4a3b3c354ee84b63eef3a0ceac822cafed
|
|
7
|
+
data.tar.gz: c5b2552f169cbc78f043441ea105c517b4fabef753469b71bd3cfef41775ac335a7bc3785faf80137d0f82a93ac1f7724b75064556a295725e6c37aad6fdf552
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.4.0] - 2026-05-29
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- `EnvLoader.dump(hash)` serializes a hash to `.env`-formatted text suitable for `parse` or for writing to a file; alphabetically sorted keys, double-quotes for values containing whitespace/`=`/`#`/quotes, backslash-escapes inner `"` and `\`, trailing newline
|
|
14
|
+
- `parse` now unescapes `\"`, `\\`, and `\n` inside double-quoted values so `parse(dump(h))` round-trips cleanly for values with inner quotes
|
|
15
|
+
|
|
10
16
|
## [0.3.0] - 2026-05-01
|
|
11
17
|
|
|
12
18
|
### Added
|
data/README.md
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
[](https://rubygems.org/gems/philiprehberger-env_loader)
|
|
5
5
|
[](https://github.com/philiprehberger/rb-env-loader/commits/main)
|
|
6
6
|
|
|
7
|
+

|
|
8
|
+
|
|
7
9
|
Multi-source environment variable loader with precedence and validation
|
|
8
10
|
|
|
9
11
|
## Requirements
|
|
@@ -88,6 +90,21 @@ Philiprehberger::EnvLoader.parse(content)
|
|
|
88
90
|
# => { "APP_HOST" => "localhost", "APP_PORT" => "3000" }
|
|
89
91
|
```
|
|
90
92
|
|
|
93
|
+
### Dump to `.env` Format
|
|
94
|
+
|
|
95
|
+
Serialize a hash back to `.env`-formatted text. Round-trips with `parse` for any input. Values that contain whitespace, `=`, `#`, or quote characters are double-quoted with inner `"` and `\` backslash-escaped. Keys are sorted alphabetically.
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
text = Philiprehberger::EnvLoader.dump(
|
|
99
|
+
'APP_HOST' => 'localhost',
|
|
100
|
+
'APP_PORT' => '3000',
|
|
101
|
+
'NOTE' => 'has "quote"'
|
|
102
|
+
)
|
|
103
|
+
# => "APP_HOST=localhost\nAPP_PORT=3000\nNOTE=\"has \\\"quote\\\"\"\n"
|
|
104
|
+
|
|
105
|
+
File.write('.env', text)
|
|
106
|
+
```
|
|
107
|
+
|
|
91
108
|
## API
|
|
92
109
|
|
|
93
110
|
| Method | Description |
|
|
@@ -96,6 +113,7 @@ Philiprehberger::EnvLoader.parse(content)
|
|
|
96
113
|
| `.validate!(*keys)` | Raise if any keys are missing or empty in ENV |
|
|
97
114
|
| `.generate_template(output:, keys:)` | Generate a .env.template file |
|
|
98
115
|
| `.parse(content)` | Parse `.env`-formatted content from a string into a hash without touching ENV |
|
|
116
|
+
| `.dump(hash)` | Serialize a hash to `.env`-formatted text (round-trips with `parse`) |
|
|
99
117
|
| `EnvLoader::Error` | Base error class for all gem errors |
|
|
100
118
|
| `EnvLoader::ValidationError` | Raised when required keys are missing or empty |
|
|
101
119
|
|
|
@@ -85,8 +85,11 @@ module Philiprehberger
|
|
|
85
85
|
|
|
86
86
|
key = key.strip
|
|
87
87
|
value = value.strip
|
|
88
|
-
value
|
|
89
|
-
|
|
88
|
+
if value.length >= 2 && value.start_with?('"') && value.end_with?('"')
|
|
89
|
+
value = value[1..-2].gsub(/\\(.)/) { Regexp.last_match(1) == 'n' ? "\n" : Regexp.last_match(1) }
|
|
90
|
+
elsif value.length >= 2 && value.start_with?("'") && value.end_with?("'")
|
|
91
|
+
value = value[1..-2]
|
|
92
|
+
end
|
|
90
93
|
result[key] = value
|
|
91
94
|
end
|
|
92
95
|
result
|
|
@@ -101,6 +104,44 @@ module Philiprehberger
|
|
|
101
104
|
end
|
|
102
105
|
private_class_method :parse_file
|
|
103
106
|
|
|
107
|
+
# Serialize a hash to `.env`-formatted text suitable for writing to a
|
|
108
|
+
# file or passing to {parse}.
|
|
109
|
+
#
|
|
110
|
+
# Keys are emitted in alphabetical order. Values that contain
|
|
111
|
+
# whitespace, `=`, `#`, or quote characters are wrapped in double
|
|
112
|
+
# quotes with inner `"` and `\` backslash-escaped. `nil` values become
|
|
113
|
+
# empty (`KEY=`). The output always ends with a single trailing
|
|
114
|
+
# newline.
|
|
115
|
+
#
|
|
116
|
+
# @example Round trip
|
|
117
|
+
# Philiprehberger::EnvLoader.parse(
|
|
118
|
+
# Philiprehberger::EnvLoader.dump('A' => '1', 'B' => 'two words')
|
|
119
|
+
# )
|
|
120
|
+
# # => { "A" => "1", "B" => "two words" }
|
|
121
|
+
#
|
|
122
|
+
# @param hash [Hash{String, Symbol => Object}] the key-value pairs to serialize
|
|
123
|
+
# @return [String] `.env`-formatted text
|
|
124
|
+
def self.dump(hash)
|
|
125
|
+
lines = hash.to_h
|
|
126
|
+
.transform_keys(&:to_s)
|
|
127
|
+
.sort_by(&:first)
|
|
128
|
+
.map { |key, value| "#{key}=#{format_value(value)}" }
|
|
129
|
+
"#{lines.join("\n")}\n"
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# @api private
|
|
133
|
+
def self.format_value(value)
|
|
134
|
+
return '' if value.nil?
|
|
135
|
+
|
|
136
|
+
str = value.to_s
|
|
137
|
+
return '' if str.empty?
|
|
138
|
+
return str unless str.match?(/[\s="'#]/)
|
|
139
|
+
|
|
140
|
+
escaped = str.gsub('\\') { '\\\\' }.gsub('"') { '\\"' }
|
|
141
|
+
%("#{escaped}")
|
|
142
|
+
end
|
|
143
|
+
private_class_method :format_value
|
|
144
|
+
|
|
104
145
|
# Apply type coercions to ENV values.
|
|
105
146
|
#
|
|
106
147
|
# @param types [Hash<String, Symbol>] key => type mapping
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: philiprehberger-env_loader
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Philip Rehberger
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-05-
|
|
11
|
+
date: 2026-05-30 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: Load environment variables from multiple .env files with configurable
|
|
14
14
|
precedence, type coercion, required key validation, default values, and template
|