A Basic YAML Config Module For Ruby

In the process of working with Albacore and creating a task to wrap around SQL Server’s SQLCmd.exe, I wanted to ensure that I could allow individual developers the ability to easily provide their own database server connection information, so that they can easily run database scripts against their local database server instance. To do this, I decided to have a little fun with YAML and some ruby metaprogramming.

The YAMLConfigBase Mixin

The end result is a little YAMLConfig module that allows you to mixin the ability to dynamically add / set some attributes on a ruby class.

   1: require 'yaml'


   3: module YAMLConfigBase

   4:     def configure(yml_file)

   5:         config = YAML::load(File.open(yml_file))

   6:         parse_config config

   7:     end    


   9:     def parse_config(config)

  10:         config.each do |key, value|

  11:             setter = "#{key}="

  12:             self.class.send(:attr_accessor, key) if !respond_to?(setter)

  13:             send setter, value

  14:         end

  15:     end

  16: end

</div> </div>

This code will read a simple name/value pair from a yaml file, loop through each of the pairs and set an attr_accessor to that name, with that value. It’s also smart enough to check and see if the attr_accessor exists, before trying to create it. So you can configure a ruby object that already has attributes set up, with this utility.

Using YAMLConfigBase

Any class that includes the YAMLConfigBase module will be able to easily load a yaml file and have the key / value pairs from that yaml file automatically set as attributes w/ values.

If we have this yml in a file called “someconfigfile.yml”

   1: my_setting: my value goes here!

</div> </div>

and we have this ruby code:

   1: require 'yamlconfigbase'


   3: class Foo

   4:   include YAMLConfigBase

   5: end


   7: f = Foo.new

   8: f.configure("someconfigfile.yml")


  10: puts f.my_setting

</div> </div>

we end up with this output (running IRB in windows command prompt):


The “Foo” class in this example only has 3 lines of code in it, yet I was able to create, set, and get a value in an attribute that I defined in a yaml file. You want to talk about DRY code? This is death-valley in the middle of a summer drought, dry. 🙂

I know it’s not super-duper-amazingly-original stuff, but it’s fun to do this kind of thing and make the rest of a framework easily configurable through yaml. It also shows the power of meta-programming and dynamic languages like ruby.

Albacore: Breaking Changes In Location Of Executables