envv 0.1.2 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 953e78210eb08257ab2065174ec9c946870786ad81e57326a90c2251c94109b1
4
- data.tar.gz: fff4477832bba43454a33260009c54db61a28f2e1c69f896f587cba43f18c516
3
+ metadata.gz: 39d9bec08e12fbe078eafec545a6ff71a646808abd537c8a8550a9add89d7464
4
+ data.tar.gz: c6e9d7c02b5460379ae1a19858d715ba77052d7f6f4772efac2d8733ff36782a
5
5
  SHA512:
6
- metadata.gz: 88bad0d1f5902143100ca1ede91645dd2d1772f13553bc4861966a7c766e81cb55327551a4aaf7b88aa21252e47144940e2227562b49bbcc532767727ce81b43
7
- data.tar.gz: 9b616de18bf3e179068dbc3104fad5b34252ab08ef2e938c2149c043c64f53759ba1e791a496dd219ca2437535751279859a9870b3bc18d08f6ac868239f617e
6
+ metadata.gz: e1dfd29c1e66010630ac4cc6ce9d8024271075a1af5c7f37991e07c678dd1360da0fa6f5606eaed3ceed0045a226257d2726b98423d4c323348018c4e3bd9c3d
7
+ data.tar.gz: fe14b778db78cd69ebdc353a1fd691c70d92419d772f0aea00450a1b66466f7b9960dd68590b4d394aa43602a16b4b9522b54adac99ec402d490d47e9afba0fd
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.1] - 2024-11-05
4
+
5
+ - Security fixes: Bumps [rexml](https://github.com/ruby/rexml) from 3.2.6 to 3.3.9.
6
+
7
+ ## [0.2.0] - 2023-09-25
8
+
9
+ - Refactoring with a ENVV::Base module to allow writing custom wrappers
10
+
11
+ ## [0.1.2] - 2023-09-19
12
+
13
+ - Fix (again…) changelog_uri in gemspec
14
+
3
15
  ## [0.1.1] - 2023-09-19
4
16
 
5
17
  - Fix changelog_uri in gemspec
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Gem Version](https://badge.fury.io/rb/envv.svg)](https://badge.fury.io/rb/envv)
2
+
1
3
  ![ENVV banner](doc/banner.svg)
2
4
 
3
5
  # ENVV
@@ -37,7 +39,7 @@ end
37
39
  ```
38
40
 
39
41
  If requirements are not satisfied, it will **raise an exception**. So be sure this validation occurs as soon as possible in the boot process of your application.
40
- In a Ruby On Rails application, you can place it in an initializer and ensure it will be executed first by naming it `app/initializers/01-envv.rb` for example, since initializers are run in alphabetical order.
42
+ In a Ruby On Rails application, you can place it in an initializer and ensure it will be executed first by naming it `config/initializers/01-envv.rb` for example, since initializers are run in alphabetical order.
41
43
 
42
44
  If environment variables are validated, you can now access their coerced value with `ENVV#fetch` method:
43
45
 
@@ -53,6 +55,49 @@ ENVV.fetch("MY_INT_VAR") # ⇒ 4000
53
55
  ENVV.fetch("MY_BOOLEAN_VAR") # ⇒ true
54
56
  ```
55
57
 
58
+ ### Adding extra features with your own ENVV wrapper
59
+
60
+ You can include [ENVV::Base](lib/envv/base.rb) in your own class or module and thus provide extra features.
61
+
62
+ With a module
63
+
64
+ ```ruby
65
+ module MyAppEnv
66
+ extend ENVV::Base
67
+
68
+ def is_dark?
69
+ fetch("IS_DARK")
70
+ end
71
+ end
72
+
73
+ MyAppEnv.build! do
74
+ required(:IS_DARK).filled(:bool)
75
+ end
76
+
77
+ # IS_DARK=True
78
+
79
+ MyAppEnv.is_dark? # => true
80
+ ```
81
+
82
+ With a class
83
+
84
+ ```ruby
85
+ class MyAppEnv
86
+ include ENVV::Base
87
+
88
+ def [](key)
89
+ fetch(key)
90
+ end
91
+ end
92
+
93
+ e = MyAppEnv.new.build! do
94
+ required(:IS_DARK).filled(:bool)
95
+ end
96
+ e["IS_DARK"] # => true
97
+ ```
98
+
99
+ Be creative !
100
+
56
101
 
57
102
  ## Development
58
103
 
data/envv.gemspec ADDED
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/envv/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "envv"
7
+ spec.version = ENVV::VERSION
8
+ spec.authors = ["Fabrice Luraine"]
9
+ spec.email = ["16@asciiland.net"]
10
+
11
+ spec.summary = "Validates environment variables requirements with a schema and gives access to their coerced values."
12
+ spec.description = ""
13
+ spec.homepage = "https://github.com/16/envv"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.0.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/16/envv"
19
+ spec.metadata["changelog_uri"] = "https://github.com/16/envv/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (File.expand_path(f) == __FILE__) ||
26
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "dry-schema", "~> 1.13"
34
+ spec.add_dependency "dry-initializer", "~> 3.1"
35
+
36
+ spec.add_development_dependency "bundler", "~> 2.4"
37
+ spec.add_development_dependency "rake", "~> 13.0"
38
+ spec.add_development_dependency "minitest", "~> 5.19"
39
+ spec.add_development_dependency "standard", "~> 1.31"
40
+
41
+ # For more information and examples about making a new gem, check out our
42
+ # guide at: https://bundler.io/guides/creating_gem.html
43
+ end
data/lib/envv/base.rb ADDED
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ENVV
4
+ module Base
5
+ # Validates ENV vars with schema rules and store coerced values in ENVV registry
6
+ # @param [Proc] A block with Dry::Schema.Params rules
7
+ # @raise [ENVV::InvalidSchemaError] if env vars requirements are not validated
8
+ # @return [ENVV]
9
+ def build!(&rules)
10
+ rules or raise ArgumentError, <<~MESSAGE
11
+ A block of schema rules is required to build ENVV.
12
+ Example:
13
+
14
+ ENVV.build! do
15
+ required(:MY_STRING_VAR).filled(:string)
16
+ required(:MY_INT_VAR).filled(:integer, gt?: 3000)
17
+ required(:MY_BOOLEAN_VAR).filled(:bool)
18
+ end
19
+
20
+ More info:
21
+
22
+ - https://dry-rb.org/gems/dry-schema
23
+ - https://github.com/16/envv
24
+
25
+ MESSAGE
26
+
27
+ @schema = ::Dry::Schema.Params(&rules)
28
+ @registry = Builder.call(ENV, @schema)
29
+ freeze
30
+ end
31
+
32
+ # @raise [ENVV::NotBuilt] error if called before ENVV built (see #build!)
33
+ # @return [Dry::Schema.Params] used to validate environment variables
34
+ def schema
35
+ @schema
36
+ end
37
+
38
+ # @raise [ENVV::NotBuilt] error if called before ENVV built (see #build!)
39
+ # @return [ENVV::Registry] Hash-like instance created at build
40
+ def registry
41
+ @registry or raise(NotBuilt)
42
+ end
43
+
44
+ # Fetch a coerced environment variable.
45
+ # This method use the same signature as Hash#fetch.
46
+ # @raise [KeyError] if `key` is not found and neither `default_value` nor a block was given.
47
+ # @overload fetch(key)
48
+ # @param key [String, Symbol]
49
+ # @return the value of the given `key` if found.
50
+ # @overload fetch(key, default_value)
51
+ # @param key [String, Symbol]
52
+ # @param default_value
53
+ # @return `default_value` if `key` is not found and no block was given
54
+ # @overload fetch(key, &block)
55
+ # @yields `key` to the block if `key` is not found and a block was given
56
+ # @return the block's return value.
57
+ def fetch(key, default_value = nil, &block)
58
+ registry.fetch(key.to_s, default_value, &block)
59
+ end
60
+ end
61
+ end
data/lib/envv/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ENVV
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/envv.rb CHANGED
@@ -1,70 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "singleton"
4
3
  require "dry/schema"
5
4
  require_relative "envv/version"
6
5
  require_relative "envv/errors"
7
6
  require_relative "envv/registry"
8
7
  require_relative "envv/builder"
8
+ require_relative "envv/base"
9
9
 
10
10
  module ENVV
11
- module_function
12
-
13
- # Validates ENV vars with schema rules and store coerced values in ENVV registry
14
- # @param [Proc] A block with Dry::Schema.Params rules
15
- # @raise [ENVV::InvalidSchemaError] if env vars requirements are not validated
16
- # @return [ENVV]
17
- def build!(&rules)
18
- rules or raise ArgumentError, <<~MESSAGE
19
- A block of schema rules is required to build ENVV.
20
- Example:
21
-
22
- ENVV.build! do
23
- required(:MY_STRING_VAR).filled(:string)
24
- required(:MY_INT_VAR).filled(:integer, gt?: 3000)
25
- required(:MY_BOOLEAN_VAR).filled(:bool)
26
- end
27
-
28
- More info:
29
-
30
- - https://dry-rb.org/gems/dry-schema
31
- - https://github.com/16/envv
32
-
33
- MESSAGE
34
-
35
- @schema = ::Dry::Schema.Params(&rules)
36
- @registry = Builder.call(ENV, @schema)
37
- freeze
38
- end
39
-
40
- # @raise [ENVV::NotBuilt] error if called before ENVV built (see #build!)
41
- # @return [Dry::Schema.Params] used to validate environment variables
42
- def schema
43
- @schema
44
- end
45
-
46
- # @raise [ENVV::NotBuilt] error if called before ENVV built (see #build!)
47
- # @return [ENVV::Registry] Hash-like instance created at build
48
- def registry
49
- @registry or raise(NotBuilt)
50
- end
51
-
52
- # Fetch a coerced environment variable.
53
- # This method use the same signature as Hash#fetch.
54
- # @raise [KeyError] if `key` is not found and neither `default_value` nor a block was given.
55
- # @overload fetch(key)
56
- # @param key [String, Symbol]
57
- # @return the value of the given `key` if found.
58
- # @overload fetch(key, default_value)
59
- # @param key [String, Symbol]
60
- # @param default_value
61
- # @return `default_value` if `key` is not found and no block was given
62
- # @overload fetch(key, &block)
63
- # @yields `key` to the block if `key` is not found and a block was given
64
- # @return the block's return value.
65
- def fetch(key, default_value = nil, &block)
66
- registry.fetch(key.to_s, default_value, &block)
67
- end
68
-
69
- public :build!, :schema, :registry, :fetch
11
+ extend Base
70
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: envv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabrice Luraine
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-19 00:00:00.000000000 Z
11
+ date: 2024-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-schema
@@ -109,7 +109,9 @@ files:
109
109
  - Rakefile
110
110
  - doc/banner.inkscape.svg
111
111
  - doc/banner.svg
112
+ - envv.gemspec
112
113
  - lib/envv.rb
114
+ - lib/envv/base.rb
113
115
  - lib/envv/builder.rb
114
116
  - lib/envv/errors.rb
115
117
  - lib/envv/registry.rb