- Add
gem 'configurator2'to your Gemfile - Run
bundle - Profit
class Application
extend Configurator
option :api_url, "https://www.myapp.com/api/v1"
options :format, :mode, :whatevs
endThis will add Application.config.api_url, which will be overridable but default
to https://www.myapp.com/api/v1. It also adds a number of options without
defaults, namely format, mode, and whatevs.
Every call to option or options adds getters and setters for these options,
but you can also use the alternate syntax by ommitting the equals sign when setting
an option.
Configurator supports three different interfaces and two setter methods:
Application.config do
api_url "https://www.some.other.app/api/v2"
format :json
endApplication.config do |config|
config.api_url = "https://www.some.other.app/api/v2"
config.format = :json
endApplication.config.api_url = "https://www.some.other.app/api/v2"
Application.config.format = :jsonOR omit the equals operators:
Application.config.mode :productionAdding a sub-configuration is simple, too, like so:
class Application
extend Configurator
option :smtp_server do
options :host, :port, :password, :username
end
endNow, you can refer to an Application's smtp_server configuration like so:
Application.config.smtp_server.host
Application.config.smtp_server.port
# etcYou can also configure a group of configuration options as a hash:
Application.config.smtp_server = {
host: "smtp.host.com",
port: "3306",
username: "user",
password: "pass"
}Just refer to a class or module's configuration setting later, pretty simply:
if Application.config.smtp_server.host
Mailer.send_email_with_options(Application.config.smtp_server)
endOr whatever.