Class: Philiprehberger::EnvValidator::Result
- Inherits:
-
Object
- Object
- Philiprehberger::EnvValidator::Result
- Defined in:
- lib/philiprehberger/env_validator/result.rb
Overview
Holds validated environment values with typed accessors.
Instance Method Summary collapse
-
#fetch(name) ⇒ Object
(also: #[])
Fetch a validated value by name.
-
#initialize(values) ⇒ Result
constructor
A new instance of Result.
-
#key?(name) ⇒ Boolean
Check if a variable was defined in the schema.
-
#keys ⇒ Array<String>
All defined variable names.
-
#slice(*names) ⇒ Hash<String, Object>
Return a subset hash of specific keys.
-
#to_h ⇒ Hash<String, Object>
All validated values.
Constructor Details
#initialize(values) ⇒ Result
Returns a new instance of Result.
8 9 10 |
# File 'lib/philiprehberger/env_validator/result.rb', line 8 def initialize(values) @values = values end |
Instance Method Details
#fetch(name) ⇒ Object Also known as: []
Fetch a validated value by name.
17 18 19 20 21 22 |
# File 'lib/philiprehberger/env_validator/result.rb', line 17 def fetch(name) key = name.to_s raise KeyError, "Unknown variable: #{key}" unless @values.key?(key) @values[key] end |
#key?(name) ⇒ Boolean
Check if a variable was defined in the schema.
35 36 37 |
# File 'lib/philiprehberger/env_validator/result.rb', line 35 def key?(name) @values.key?(name.to_s) end |
#keys ⇒ Array<String>
Returns all defined variable names.
27 28 29 |
# File 'lib/philiprehberger/env_validator/result.rb', line 27 def keys @values.keys end |
#slice(*names) ⇒ Hash<String, Object>
Return a subset hash of specific keys.
43 44 45 46 |
# File 'lib/philiprehberger/env_validator/result.rb', line 43 def slice(*names) string_keys = names.map(&:to_s) @values.slice(*string_keys) end |
#to_h ⇒ Hash<String, Object>
Returns all validated values.
49 50 51 |
# File 'lib/philiprehberger/env_validator/result.rb', line 49 def to_h @values.dup end |