Configuration made easy

Recently I was writing a simple program (more a script than a full-fledged program) to report the number of commits for each dev on all repos owned by a given organization.

Following the will to develop something extensible I set up a CLI tool (actually a gem) based on the famous gem Thor.

In order to have full power to configure my gem I choose to rely on another gem called configuration.

As stated inside its README it

provides a unified approach to do configurations using the MyGem.configure do ... end idiom…

Many implementation of this idiom have been extensively described through the years. Two examples are Dan Crock’s MyGem.configure Block and Brandon Hilkert’s Ruby Gem Configuration Patterns.

Likewise the latter two implementations, the configuration gem also grants the possibility to configure your gem by passing an hash. This is accomplished by using its from_h method.

This is particularly useful when you need to create a configuration composed by a set of a priori unknown keys.

There is however something to take into consideration if you need to use this feature. Before version 2.1.0 the from_h method doesn’t work with hashes composed by string keys. It only works with symbol keys.

I discovered this issue while trying to set up a dynamic configuration and thanks to the creator of the configuration gem the “problem” was successfully solved as stated by this issue.

So now, happy configuration to everyone! 😛

Cheers!

Leave a Reply

wpDiscuz