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, #reset_tried, resolvers, #set_tried, #tried?, #try_resolve

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
# File 'lib/opto/resolvers/environment_variable.rb', line 10

def resolve
  raise ArgumentError, "Environment variable name not set" if hint.nil?
  val = ENV[hint.to_s]
  return nil if val.nil?
  case val
  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
end