Module: Config
- Defined in:
- lib/env-conf.rb,
lib/env-conf/version.rb
Constant Summary collapse
- VERSION =
'0.0.5'
- @@defaults =
{}
- @@dotenv =
{}
Class Method Summary collapse
-
.[](name) ⇒ String
Get a Config value.
-
.app_deploy ⇒ String
The ‘APP_DEPLOY` env var is used to identify which deploy of the codebase is running in librato.
-
.app_env ⇒ Symbol
The ‘RACK_ENV` env var is used to identify the app mode.
-
.app_name ⇒ String
The ‘APP_NAME` env 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.
-
.dotenv! ⇒ Object
Loads a “.env” file, using Dotenv to parse but not fuck up the ENV.
-
.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_ENV` environment variable is used to determine whether the service is in production mode or not.
-
.reset! ⇒ Object
Reset defaults values.
-
.test? ⇒ Bool
The ‘RACK_ENV` environment 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’
31 32 33 34 35 |
# File 'lib/env-conf.rb', line 31 def self.[](name) var_name = name.to_s.upcase default_name = name.to_s.downcase.to_sym ENV[var_name] || @@dotenv[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.
112 113 114 |
# File 'lib/env-conf.rb', line 112 def self.app_deploy env("APP_DEPLOY") end |
.app_env ⇒ Symbol
The ‘RACK_ENV` env var is used to identify the app mode
119 120 121 |
# File 'lib/env-conf.rb', line 119 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.
103 104 105 |
# File 'lib/env-conf.rb', line 103 def self.app_name env("APP_NAME") end |
.array(name) ⇒ Array
Comma-separated words converted to an array.
156 157 158 |
# File 'lib/env-conf.rb', line 156 def self.array(name) env(name).to_s.split(',') end |
.bool?(name) ⇒ bool
An environment variable converted to a bool.
165 166 167 |
# File 'lib/env-conf.rb', line 165 def self.bool?(name) self[name] == true || self[name] == 'true' end |
.database_url(kind = '') ⇒ Object
The database URL from the environment.
135 136 137 138 |
# File 'lib/env-conf.rb', line 135 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
58 59 60 |
# File 'lib/env-conf.rb', line 58 def self.default(key, value) @@defaults[key.to_s.downcase.to_sym] = value end |
.defaults ⇒ Hash
Get all the defaults
64 65 66 |
# File 'lib/env-conf.rb', line 64 def self.defaults @@defaults end |
.development? ⇒ Bool
Returns True if the service is in development mode.
95 96 97 |
# File 'lib/env-conf.rb', line 95 def self.development? self['RACK_ENV'] == 'development' end |
.dotenv! ⇒ Object
Loads a “.env” file, using Dotenv to parse but not fuck up the ENV
187 188 189 190 191 192 193 194 195 |
# File 'lib/env-conf.rb', line 187 def self.dotenv! return if Config.production? require 'dotenv' ['.env','.env.local',".env.#{Config[:rack_env]}",".env.#{Config[:rack_env]}.local"].each do |filename| if File.exists?(filename) @@dotenv.update(Dotenv::Parser.call(File.read(filename))) end end end |
.env(name) ⇒ String
An environment variable.
49 50 51 |
# File 'lib/env-conf.rb', line 49 def self.env(name) self[name] end |
.env!(name) ⇒ String
An environment variable.
74 75 76 |
# File 'lib/env-conf.rb', line 74 def self.env!(name) self[name] || raise("missing #{name}") end |
.int(name) ⇒ Fixnum
An environment variable converted to a Fixnum.
146 147 148 |
# File 'lib/env-conf.rb', line 146 def self.int(name) self[name] && Integer(self[name]) end |
.port ⇒ Fixnum
The port to listen on for web requests.
126 127 128 |
# File 'lib/env-conf.rb', line 126 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.
82 83 84 |
# File 'lib/env-conf.rb', line 82 def self.production? self['RACK_ENV'] == 'production' end |
.reset! ⇒ Object
Reset defaults values
38 39 40 41 |
# File 'lib/env-conf.rb', line 38 def self.reset! @@defaults = {} @@dotenv = {} end |
.test? ⇒ Bool
The ‘RACK_ENV` environment variable is used to determine whether the service is in test mode or not.
90 91 92 |
# File 'lib/env-conf.rb', line 90 def self.test? self['RACK_ENV'] == 'test' end |
.time(name) ⇒ Time
An environment variable converted to a time.
174 175 176 |
# File 'lib/env-conf.rb', line 174 def self.time(name) self[name] && Time.parse(self[name]) end |
.uri(name) ⇒ URI
An environment variable converted to a URI.
182 183 184 |
# File 'lib/env-conf.rb', line 182 def self.uri(name) self[name] && URI.parse(self[name]) end |