Los Techies : Blogs about software and anything tech!

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'
   2:  
   3: module YAMLConfigBase
   4:     def configure(yml_file)
   5:         config = YAML::load(File.open(yml_file))
   6:         parse_config config
   7:     end    
   8:     
   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

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!

and we have this ruby code:

   1: require 'yamlconfigbase'
   2:  
   3: class Foo
   4:   include YAMLConfigBase
   5: end
   6:  
   7: f = Foo.new
   8: f.configure("someconfigfile.yml")
   9:  
  10: puts f.my_setting

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

image

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.

Kick It on DotNetKicks.com
Posted Oct 05 2009, 12:39 PM by derick.bailey

Comments

Dale Smith wrote re: A Basic YAML Config Module For Ruby
on 10-05-2009 2:13 PM

I dig where you're going with this, but please don't call it "YAMLConfigBase".  Nothing's actually inheriting this module, just mixing it in, right?

derick.bailey wrote re: A Basic YAML Config Module For Ruby
on 10-05-2009 2:19 PM

@Dale,

Thanks!

i debated on the word "base" for a while and wasn't really sure it was the right thing to do... stuck with it because of my old-school static language habits. you're right, though... I'll get rid of "base" in the Albacore framework pretty soon.

DotNetBurner - SQL Server wrote A Basic YAML Config Module For Ruby
on 10-05-2009 8:42 PM

DotNetBurner - burning hot .net content

Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems