Module: Config
- Defined in:
- lib/env-conf.rb,
lib/env-conf/version.rb
Constant Summary collapse
- VERSION =
'0.0.2'- @@defaults =
{}
Class Method Summary collapse
-
.[](name) ⇒ String
Get a Config value.
-
.app_deploy ⇒ String
The
APP_DEPLOYenv var is used to identify which deploy of the codebase is running in librato. -
.app_env ⇒ Symbol
The
RACK_ENVenv var is used to identify the app mode. -
.app_name ⇒ String
The
APP_NAMEenv var is used to identify which codebase is running in librato. -
.array(name) ⇒ Array
Comma-separated words converted to an array.
-
.bool?(name) ⇒ bool
An environment variable converted to a bool.
-
.database_url(kind = '') ⇒ Object
The database URL from the environment.
-
.default(key, value) ⇒ String
Set a default Defaults are supplied when accessing via Config.
-
.defaults ⇒ Hash
Get all the defaults.
-
.development? ⇒ Bool
True if the service is in development mode.
-
.env(name) ⇒ String
An environment variable.
-
.env!(name) ⇒ String
An environment variable.
-
.int(name) ⇒ Fixnum
An environment variable converted to a Fixnum.
-
.port ⇒ Fixnum
The port to listen on for web requests.
-
.production? ⇒ Bool
The
RACK_ENVenvironment variable is used to determine whether the service is in production mode or not. -
.reset! ⇒ Object
Reset defaults values.
-
.test? ⇒ Bool
The
RACK_ENVenvironment variable is used to determine whether the service is in test mode or not. -
.time(name) ⇒ Time
An environment variable converted to a time.
-
.uri(name) ⇒ URI
An environment variable converted to a URI.
Class Method Details
.[](name) ⇒ String
Get a Config value
This is the preferred and uniform way to access config vars because defaults are included in the lookup
Uses defaults if available. Converts upper-case ENV var names to lower-case default names.
Order of precedence is: 1) app’s local ENV 2) default values
Config == nil
Config.default(:foo, ‘bar’) Config == ‘bar’
28 29 30 31 32 |
# File 'lib/env-conf.rb', line 28 def self.[](name) var_name = name.to_s.upcase default_name = name.to_s.downcase.to_sym ENV[var_name] || @@defaults[default_name] end |
.app_deploy ⇒ String
The APP_DEPLOY env var is used to identify which deploy of the codebase is running in librato. This usually matches the name of the environment such as local, production, staging, etc.
108 109 110 |
# File 'lib/env-conf.rb', line 108 def self.app_deploy env("APP_DEPLOY") end |
.app_env ⇒ Symbol
The RACK_ENV env var is used to identify the app mode
115 116 117 |
# File 'lib/env-conf.rb', line 115 def self.app_env env!("RACK_ENV").to_sym end |
.app_name ⇒ String
The APP_NAME env var is used to identify which codebase is running in librato. This usually matches the name of the repository.
99 100 101 |
# File 'lib/env-conf.rb', line 99 def self.app_name env("APP_NAME") end |
.array(name) ⇒ Array
Comma-separated words converted to an array.
152 153 154 |
# File 'lib/env-conf.rb', line 152 def self.array(name) env(name).to_s.split(',') end |
.bool?(name) ⇒ bool
An environment variable converted to a bool.
161 162 163 |
# File 'lib/env-conf.rb', line 161 def self.bool?(name) self[name] == true || self[name] == 'true' end |
.database_url(kind = '') ⇒ Object
The database URL from the environment.
131 132 133 134 |
# File 'lib/env-conf.rb', line 131 def self.database_url(kind = '') kind = "#{kind}_".upcase unless kind.empty? env!("#{kind}DATABASE_URL") end |
.default(key, value) ⇒ String
Set a default Defaults are supplied when accessing via Config
54 55 56 |
# File 'lib/env-conf.rb', line 54 def self.default(key, value) @@defaults[key.to_s.downcase.to_sym] = value end |
.defaults ⇒ Hash
Get all the defaults
60 61 62 |
# File 'lib/env-conf.rb', line 60 def self.defaults @@defaults end |
.development? ⇒ Bool
Returns True if the service is in development mode.
91 92 93 |
# File 'lib/env-conf.rb', line 91 def self.development? self['RACK_ENV'] == 'development' end |
.env(name) ⇒ String
An environment variable.
45 46 47 |
# File 'lib/env-conf.rb', line 45 def self.env(name) self[name] end |
.env!(name) ⇒ String
An environment variable.
70 71 72 |
# File 'lib/env-conf.rb', line 70 def self.env!(name) self[name] || raise("missing #{name}") end |
.int(name) ⇒ Fixnum
An environment variable converted to a Fixnum.
142 143 144 |
# File 'lib/env-conf.rb', line 142 def self.int(name) self[name] && self[name].to_i end |
.port ⇒ Fixnum
The port to listen on for web requests.
122 123 124 |
# File 'lib/env-conf.rb', line 122 def self.port env!("PORT").to_i end |
.production? ⇒ Bool
The RACK_ENV environment variable is used to determine whether the service is in production mode or not.
78 79 80 |
# File 'lib/env-conf.rb', line 78 def self.production? self['RACK_ENV'] == 'production' end |
.reset! ⇒ Object
Reset defaults values
35 36 37 |
# File 'lib/env-conf.rb', line 35 def self.reset! @@defaults = {} end |
.test? ⇒ Bool
The RACK_ENV environment variable is used to determine whether the service is in test mode or not.
86 87 88 |
# File 'lib/env-conf.rb', line 86 def self.test? self['RACK_ENV'] == 'test' end |
.time(name) ⇒ Time
An environment variable converted to a time.
170 171 172 |
# File 'lib/env-conf.rb', line 170 def self.time(name) self[name] && Time.parse(self[name]) end |
.uri(name) ⇒ URI
An environment variable converted to a URI.
178 179 180 |
# File 'lib/env-conf.rb', line 178 def self.uri(name) self[name] && URI.parse(self[name]) end |