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

Class Method Details

.development?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/startback/support/env.rb', line 49

def development?
  ENV['RACK_ENV'] =~ /^dev/
end

.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.

Raises:



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

.production?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/startback/support/env.rb', line 44

def production?
  ENV['RACK_ENV'] =~ /^prod/
end

.staging?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/startback/support/env.rb', line 39

def staging?
  ENV['RACK_ENV'] == 'staging'
end