envv 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 953e78210eb08257ab2065174ec9c946870786ad81e57326a90c2251c94109b1
4
- data.tar.gz: fff4477832bba43454a33260009c54db61a28f2e1c69f896f587cba43f18c516
3
+ metadata.gz: 0a9575c3a4575817075da63460f3f825073b1cf60924bb15448b5942292aa5ee
4
+ data.tar.gz: 007112b9e413b0583ab1e62130c65d4901f3cf7aeecef643d85e5988714bb664
5
5
  SHA512:
6
- metadata.gz: 88bad0d1f5902143100ca1ede91645dd2d1772f13553bc4861966a7c766e81cb55327551a4aaf7b88aa21252e47144940e2227562b49bbcc532767727ce81b43
7
- data.tar.gz: 9b616de18bf3e179068dbc3104fad5b34252ab08ef2e938c2149c043c64f53759ba1e791a496dd219ca2437535751279859a9870b3bc18d08f6ac868239f617e
6
+ metadata.gz: 604ee5f6770bf4322c0a41dd835fdd7ac6d55cb7123624acf73ef8450e6a914a934c4cf0d5adfe6a49c9ba1c20fb7bbd7e6961994efedc359d43a38a84c1b9d0
7
+ data.tar.gz: d623fcec7868d59d7736cfe68222d6dcd40a132f4daf75e7dc8f993cb7017c5718fd8ba2390548f7dabd82aa53a046875993f4a37afa16cc659c97e1b3a11660
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2023-09-25
4
+
5
+ - Refactoring with a ENVV::Base module to allow writing custom wrappers
6
+
7
+ ## [0.1.2] - 2023-09-19
8
+
9
+ - Fix (again…) changelog_uri in gemspec
10
+
3
11
  ## [0.1.1] - 2023-09-19
4
12
 
5
13
  - 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
@@ -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/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.0"
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.0
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: 2023-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-schema
@@ -110,6 +110,7 @@ files:
110
110
  - doc/banner.inkscape.svg
111
111
  - doc/banner.svg
112
112
  - lib/envv.rb
113
+ - lib/envv/base.rb
113
114
  - lib/envv/builder.rb
114
115
  - lib/envv/errors.rb
115
116
  - lib/envv/registry.rb