Module: Startback::Support::Env
- Defined in:
- lib/startback/support/env.rb
Overview
This method provides the ‘env` and `env!` methods that help querying environment variables easily.
Class Method Summary collapse
-
.env(key, default = nil, &bl) ⇒ Object
Returns an environment variable or the default value passed as second argument.
-
.env!(key, default = nil, &bl) ⇒ Object
Returns an environment variable or raise an error if not set.
Class Method Details
.env(key, default = nil, &bl) ⇒ Object
Returns an environment variable or the default value passed as second argument.
The result is always a String with no leading/trailing spaces.
If a block is given, the environment variable is yield and the result of the block returned.
31 32 33 34 35 36 |
# File 'lib/startback/support/env.rb', line 31 def env(key, default = nil, &bl) v = ENV[key].to_s.strip v = v.empty? ? default : v v = bl.call(v) if bl && v v end |
.env!(key, default = nil, &bl) ⇒ Object
Returns an environment variable or raise an error if not set.
The result is always a String with no leading/trailing spaces.
If a block is given, the environment variable is yield and the result of the block returned.
15 16 17 18 19 20 |
# File 'lib/startback/support/env.rb', line 15 def env!(key, default = nil, &bl) v = ENV[key].to_s.strip raise Startback::Error, "Missing ENV var `#{key}`" if v.empty? env(key, default, &bl) end |