0.0
No commit activity in last 3 years
No release in over 3 years
A mixin to add a .config method and options with defaults to any library
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Configurator

Add simple configuration to any Ruby class

Installation

  1. Add gem 'configurator2' to your Gemfile
  2. Run bundle
  3. Profit

Usage

Mix into any Ruby class and add options

class Application
  extend Configurator
  option :api_url, "https://www.myapp.com/api/v1"
  options :format, :mode, :whatevs
end

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

Configure your class

Configurator supports three different interfaces and two setter methods:

Block configuration with implicit configuration object
Application.config do
  api_url "https://www.some.other.app/api/v2"
  format :json
end
Block configuration with passed configuration object
Application.config do |config|
  config.api_url = "https://www.some.other.app/api/v2"
  config.format = :json
end
Direct configuration
Application.config.api_url = "https://www.some.other.app/api/v2"
Application.config.format = :json

OR omit the equals operators:

Application.config.mode :production

Sub-configurations

Adding a sub-configuration is simple, too, like so:

class Application
  extend Configurator
  option :smtp_server do
    options :host, :port, :password, :username
  end
end

Now, you can refer to an Application's smtp_server configuration like so:

Application.config.smtp_server.host
Application.config.smtp_server.port
# etc

You 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"
}

Observe your users' configuration choices later

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)
end

Or whatever.