If you’ve been with me from the beginning of this blog, you’ll know I started off by building something that I called the “BorgBox”, which was a home PVR based on a Mac mini and MythTV. After moving to Boxee and being frustrated with the lack of a database, I finally (for now!) have settled on the OpenElec build of XBMC. Of course, there is scope for a lot of additional, offline tools, and since XBMC is very picky about directory structure etc. I wrote tools to aid me in moving files around. These have led to the creation of a number of small utility libraries, the first of which I’ve released as a gem on rubygems.
This first gem allows for multiple YAML configuration files to be used within a class by mixing in a YAMLConfig module and setting potential directories that the file could be included in, such as ["~/.appname","/etc/appname"]. This allow the configuration file to be determined with either early or late loading depending on whether the application/service needs to continue running for longer than the life of the configuration file data.
To use it, you would create code such as the following:
require 'blackrat_yaml_config' class TestApplication include YAMLConfig config_directories :etc=>['~/.test_application","/etc/test_application"] config_files :global, :types end
The relevant items are:
This sets the search path for the configuration files. Like any search path, you start at the first, and stop as soon as you find the relevant file.
This is a list of the files that contain configuration information. As default they are postfixed with a “.yml” extension. Once they are registered, they create a methods which returns the yaml file content directly.
So if there is an entry
in the “global.yml” file, you would refer to it as:
This default allows for dynamic reloading of the configuration file. The config_files method is an alias for dynamic_config_files. There is a corresponding static_config_files which places the content into the method. So if you don’t want the flexibility of reloading the yaml file every time you refer to it, specifying
static_config_files :global, :types
will load it on the creation of the method rather than when the method is called.
I’ve heard the arguments for and against using yaml for config. To those who say “Use ruby”, I disagree. There’s more scope for using the config files across languages if they aren’t in a particular language and can be easily parsed by multiple applications. Most of my development life has been spent in mixed language environments and now is either Ruby/C/C++ or Ruby/Java, and I don’t see that changing anytime soon.
Hope you enjoy this first gem extracted from the “BorgBox” set. There are many more to come.