Class: UserChoices::EnvironmentSource
- Inherits:
-
AbstractSource
- Object
- Hash
- AbstractSource
- UserChoices::EnvironmentSource
- Defined in:
- lib/user-choices/sources.rb
Overview
Describe the environment as a source of choices.
Instance Attribute Summary
Attributes inherited from AbstractSource
Instance Method Summary collapse
-
#fill ⇒ Object
:nodoc:.
-
#mapping(map) ⇒ Object
Some environment variables have names you don’t like.
-
#source ⇒ Object
:nodoc:.
-
#with_prefix(prefix) ⇒ Object
Environment variables beginning with prefix (a string) are considered to be user choices relevant to this script.
Methods inherited from AbstractSource
#adjust, #apply, #each_conversion, #initialize
Constructor Details
This class inherits a constructor from UserChoices::AbstractSource
Instance Method Details
#fill ⇒ Object
:nodoc:
104 105 106 107 108 |
# File 'lib/user-choices/sources.rb', line 104 def fill # :nodoc: @external_names.each { | key, env_var | self[key] = ENV[env_var] if ENV.has_key?(env_var) } end |
#mapping(map) ⇒ Object
Some environment variables have names you don’t like. For example, $HOME might be annoying because of the uppercase. Also, if most of your program’s environment variables have some prefix (see with_prefix) but you also want to use $HOME, you need a way to do that. You can satisfy both desires with
EnvironmentSource.new.with_prefix("my_").mapping(:home => "HOME")
139 140 141 142 |
# File 'lib/user-choices/sources.rb', line 139 def mapping(map) @external_names.merge!(map) self end |
#source ⇒ Object
:nodoc:
145 146 147 |
# File 'lib/user-choices/sources.rb', line 145 def source # :nodoc: "the environment" end |
#with_prefix(prefix) ⇒ Object
Environment variables beginning with prefix (a string) are considered to be user choices relevant to this script. Everything after the prefix names a choice (that is, a symbol). Dashes are converted to underscores. Examples:
-
Environment variable
prefix-my-choicewith prefix"prefix-" is choice <tt>:my_choice. -
Environment variable
PREFIX_FOOwith prefix"PREFIX_" is choice <tt>:FOO
If you want an array of strings, separate the values by commas: ENV_VAR=a,b,c There’s currently no way to escape a comma and no cleverness about quotes or whitespace.
122 123 124 125 126 127 128 129 130 |
# File 'lib/user-choices/sources.rb', line 122 def with_prefix(prefix) matches = ENV.collect do | env_var, ignored_value | if /^#{prefix}(.+)/ =~ env_var [$1.to_inputable_sym, env_var] end end @external_names.merge!(Hash[*matches.compact.flatten]) self end |