Class: Opto::Resolvers::Env

Inherits:
Opto::Resolver show all
Defined in:
lib/opto/resolvers/environment_variable.rb

Overview

Find a value using Environment.

Hint should be a name of environment variable, such as 'HOME'

Numbers will be converted to fixnums, “true” and “false” will be converted to booleans.

Instance Attribute Summary

Attributes inherited from Opto::Resolver

#hint, #option

Instance Method Summary collapse

Methods inherited from Opto::Resolver

for, inherited, #initialize, origin, #origin, resolvers

Constructor Details

This class inherits a constructor from Opto::Resolver

Instance Method Details

#resolveObject

Raises:

  • (ArgumentError)

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/opto/resolvers/environment_variable.rb', line 10

def resolve
  raise ArgumentError, "Environment variable name not set" if hint.nil?
  Array(hint).each do |hint|
    val = ENV[hint.to_s]
    val = case val
    when NilClass then nil
    when /\A\d+\z/ then val.to_i
    when /\Atrue\z/ then true
    when /\Afalse\z/ then false
    when /\A(?:null|nil)\z/ then nil
    else val
    end
    return val unless val.nil?
  end
  nil
end