Class: Philiprehberger::EnvValidator::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/env_validator/result.rb

Overview

Holds validated environment values with typed accessors.

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Result

Returns a new instance of Result.

Parameters:

  • values (Hash<String, Object>)

    validated and cast values



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.

Parameters:

  • name (Symbol, String)

    the variable name

Returns:

  • the cast value

Raises:

  • (KeyError)

    if the name was not defined in the schema



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.

Parameters:

  • name (Symbol, String)

    the variable name

Returns:

  • (Boolean)


35
36
37
# File 'lib/philiprehberger/env_validator/result.rb', line 35

def key?(name)
  @values.key?(name.to_s)
end

#keysArray<String>

Returns all defined variable names.

Returns:

  • (Array<String>)

    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.

Parameters:

  • names (Array<Symbol, String>)

    the variable names to include

Returns:

  • (Hash<String, Object>)


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_hHash<String, Object>

Returns all validated values.

Returns:

  • (Hash<String, Object>)

    all validated values



49
50
51
# File 'lib/philiprehberger/env_validator/result.rb', line 49

def to_h
  @values.dup
end