Skip to content

Making your Plugin or Gem configurable

Recently I added a configuration mechanism to Webrat. It was surprisingly easy, and mainly copied from rails core. I would suggest adding somthing like this to any plugin that has more than a few features or ones that users have asked to have turned off.

First off you’re going to have to create the actual configuration object. There are a few good ways to do this. One is to use a config module, another is to create a configuration object that is accessible via a singleton method.

I’m going to go with the second one, a configuration object.

Toss this one in lib/configuration.rb (A simplification of Code | RDoc)

module Plugin
  # Configures Plugin.
  def self.configure(configuration =
    yield configuration if block_given?
    @@configuration = configuration
  def self.configuration # :nodoc:
    @@configuration ||=

  # Plugin can be configured using the Plugin.configure method. For example:
  #   Plugin.configure do |config|
  #     config.show_whiny_errors = false
  #   end
  class Configuration
    # Should whiny error messages be shown?
    attr_writer :show_whiny_errors

    def initialize # :nodoc:
      # set your defaults in here
      self.show_whiny_errors = true
      # put as much as you want in here
    # some syntactic sugar for you, the coder
    def show_whiny_errors? #:nodoc:
      @show_whiny_erorrs ? true : false

Okay, now we need to test the config object itself. This is why it’s nice to make an object just to house the config, it’s easy to test. What do we test? Well defaults and accessors for two!

(The following lifted from Code) (sorry this is in rspec, it’s not hard to do in test::unit)

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
describe Plugin::Configuration do
  # define matchers for testing
  predicate_matchers[:show_whiny_errors] = :show_whiny_errors?

  it 'should show whiny errors by default' do
    config =
    config.should show_whiny_errors?
  it 'should be configurable with a block' do
    Plugin.configure do |config|
      config.show_whiny_errors = false
    config = Plugin.configuration
    config.should_not show_whiny_errors?

Now we need to do some stuff to make it nicer for our other users to test. Put the following in your test_helper or spec helper. It will allow you to clear your config after each test. Nice to have to to avoid messy test issues.

(The following lifted from Code)

module Plugin
  @@previous_config = nil
  def self.cache_config_for_test
    @@previous_config = Plugin.configuration.clone
  def self.reset_for_test
    @@configuration = @@previous_config if @@previous_config

# configure your test runner / spec runner to always clear the config
Spec::Runner.configure do |config|
  config.before :each do
  config.after :each do

This last bit is somewhat hard to do in test::unit as it is harder to hook into the setup (you only get one in the call chain). I’m willing to take some help on cleaning this one up for test::unit. Currently I have just been putting it in the setup / teardown for each test file.

Finally you need to make use of this in tests, fortunately that is quite easy. Just:

describe SomeObject do
    it 'it shouldn't do it when whiny nils are off' do
      Plugin.configure do |config|
        config.show_whiny_errors = false

Finally, anywhere in your plugin that you think somthing is whiny, just check the config before using it like this:

log("you should really fix this") if Plugin.configuration.show_whiny_errors?


  1. I rarely write responses, but i did a few searching and
    wound up here confabulus › Making your Plugin or Gem configurable.
    And I actually do have 2 questions for you if you usually do not mind.
    Is it only me or does it seem like a few of the remarks appear like they are coming from brain dead visitors?

    😛 And, if you are posting at additional online sites, I’d like to follow everything new you have to post.
    Would you make a list of the complete urls of all your communal
    pages like your linkedin profile, Facebook page or twitter feed?

    Monday, May 12, 2014 at 12:53 pm | Permalink
  2. Nahro wrote:

    on the box. It was incredible. The lower bakcurognd allowed me to see business so clearly and close up even if they equipment I was seeing were close. I was looking at a neighbor’s car down the street and I may possibly read the expiration date of state inspection sticker as clear as if I was 2 inches from it. I showed a friend of mine the scope and he was pretty impressed too. His boss has some land everywhere we used it for watching deer going to the feeder so we don’t have to scare them off. I tried it to look at the moon last weekend when the moon was nearly full it that was pretty cool. I have never used an astronomical telescope previous to so I have nothing to compare it to but the moon filled the entire viewing and was very clear and I have never seen it better in a photo in a magazine. Although it is not small it did everything I looked-for and was not too heavy. I highly recommend this scope to anyone especially if you want the real deal and not get ripped off by paying out the noise for a lesser scope.

    Sunday, December 20, 2015 at 12:59 pm | Permalink

Post a Comment

Your email is never published nor shared.