Module: ENVV::Base
- Included in:
- ENVV
- Defined in:
- lib/envv/base.rb
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.
Instance Method Details
#build!(&rules) ⇒ ENVV
Validates ENV vars with schema rules and store coerced values in ENVV registry
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/envv/base.rb', line 9 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.
57 58 59 |
# File 'lib/envv/base.rb', line 57 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.
40 41 42 |
# File 'lib/envv/base.rb', line 40 def registry @registry or raise(NotBuilt) end |
#schema ⇒ Dry::Schema.Params
Returns used to validate environment variables.
34 35 36 |
# File 'lib/envv/base.rb', line 34 def schema @schema end |