Class: VagrantPlugins::ProxyConf::Config::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-proxyconf/config/key.rb

Overview

Configuration key specifications

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Key

Returns a new instance of Key.

Parameters:

  • name (Symbol, String)

    the key name

  • opts (Hash) (defaults to: {})

    optional key properties

Options Hash (opts):

  • :default (String, nil) — default: nil

    the default value

  • :env_var (String, nil) — default: nil

    the environment variable that overrides the configuration



20
21
22
23
24
# File 'lib/vagrant-proxyconf/config/key.rb', line 20

def initialize(name, opts = {})
  @name    = name.to_sym
  @default = opts[:default]
  @env_var = opts[:env_var]
end

Instance Attribute Details

#defaultString? (readonly)

Returns the default value for the key.

Returns:

  • (String, nil)

    the default value for the key



10
11
12
# File 'lib/vagrant-proxyconf/config/key.rb', line 10

def default
  @default
end

#env_varString? (readonly)

Returns the environment variable name.

Returns:

  • (String, nil)

    the environment variable name



13
14
15
# File 'lib/vagrant-proxyconf/config/key.rb', line 13

def env_var
  @env_var
end

#nameSymbol (readonly)

Returns the configuration key name.

Returns:

  • (Symbol)

    the configuration key name



7
8
9
# File 'lib/vagrant-proxyconf/config/key.rb', line 7

def name
  @name
end

Instance Method Details

#value_from_env_var {|default| ... } ⇒ String

The value from the environment variable or the return value of the block or #default

Yields:

  • if environment variable is not specified or set

Yield Parameters:

  • default (String, nil)

    the #default value of the key

Yield Returns:

  • (String, nil)

    the default value to be returned

Returns:

  • (String)

    the value from the environment variable or the return value of the block or #default



31
32
33
34
35
36
37
38
39
# File 'lib/vagrant-proxyconf/config/key.rb', line 31

def value_from_env_var
  if env_var && ENV.key?(env_var)
    ENV[env_var]
  elsif block_given?
    yield default
  else
    default
  end
end