Module: ENVV
- Defined in:
- lib/envv.rb,
lib/envv/errors.rb,
lib/envv/builder.rb,
lib/envv/version.rb,
lib/envv/registry.rb
Defined Under Namespace
Classes: Builder, Error, InvalidEnvError, InvalidSchemaError, NotBuilt, Registry, ValidationError
Constant Summary collapse
- VERSION =
"0.1.1"
Class Method Summary collapse
-
.build!(&rules) ⇒ ENVV
Validates ENV vars with schema rules and store coerced values in ENVV registry.
-
.fetch(key, default_value = nil, &block) ⇒ Object
Fetch a coerced environment variable.
-
.registry ⇒ ENVV::Registry
Hash-like instance created at build.
-
.schema ⇒ Dry::Schema.Params
Used to validate environment variables.
Instance Method Summary collapse
-
#build!(&rules) ⇒ ENVV
Validates ENV vars with schema rules and store coerced values in ENVV registry.
-
#fetch(key, default_value = nil, &block) ⇒ Object
Fetch a coerced environment variable.
-
#registry ⇒ ENVV::Registry
Hash-like instance created at build.
-
#schema ⇒ Dry::Schema.Params
Used to validate environment variables.
Class Method Details
.build!(&rules) ⇒ ENVV
Validates ENV vars with schema rules and store coerced values in ENVV registry
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/envv.rb', line 17 def build!(&rules) rules or raise ArgumentError, <<~MESSAGE A block of schema rules is required to build ENVV. Example: ENVV.build! do required(:MY_STRING_VAR).filled(:string) required(:MY_INT_VAR).filled(:integer, gt?: 3000) required(:MY_BOOLEAN_VAR).filled(:bool) end More info: - https://dry-rb.org/gems/dry-schema - https://github.com/16/envv MESSAGE @schema = ::Dry::Schema.Params(&rules) @registry = Builder.call(ENV, @schema) freeze end |
.fetch(key) ⇒ Object .fetch(key, default_value) ⇒ Object .fetch(key, &block) ⇒ Object
Fetch a coerced environment variable.
This method use the same signature as Hash#fetch.
65 66 67 |
# File 'lib/envv.rb', line 65 def fetch(key, default_value = nil, &block) registry.fetch(key.to_s, default_value, &block) end |
.registry ⇒ ENVV::Registry
Returns Hash-like instance created at build.
48 49 50 |
# File 'lib/envv.rb', line 48 def registry @registry or raise(NotBuilt) end |
.schema ⇒ Dry::Schema.Params
Returns used to validate environment variables.
42 43 44 |
# File 'lib/envv.rb', line 42 def schema @schema end |
Instance Method Details
#build!(&rules) ⇒ ENVV
Validates ENV vars with schema rules and store coerced values in ENVV registry
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/envv.rb', line 17 def build!(&rules) rules or raise ArgumentError, <<~MESSAGE A block of schema rules is required to build ENVV. Example: ENVV.build! do required(:MY_STRING_VAR).filled(:string) required(:MY_INT_VAR).filled(:integer, gt?: 3000) required(:MY_BOOLEAN_VAR).filled(:bool) end More info: - https://dry-rb.org/gems/dry-schema - https://github.com/16/envv MESSAGE @schema = ::Dry::Schema.Params(&rules) @registry = Builder.call(ENV, @schema) freeze end |
#fetch(key) ⇒ Object #fetch(key, default_value) ⇒ Object #fetch(key, &block) ⇒ Object
Fetch a coerced environment variable.
This method use the same signature as Hash#fetch.
65 66 67 |
# File 'lib/envv.rb', line 65 def fetch(key, default_value = nil, &block) registry.fetch(key.to_s, default_value, &block) end |
#registry ⇒ ENVV::Registry
Returns Hash-like instance created at build.
48 49 50 |
# File 'lib/envv.rb', line 48 def registry @registry or raise(NotBuilt) end |
#schema ⇒ Dry::Schema.Params
Returns used to validate environment variables.
42 43 44 |
# File 'lib/envv.rb', line 42 def schema @schema end |