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.


Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in Albacore, Principles and Patterns, Ruby. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://creedcultcode.blogspot.com Dale Smith

    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?

  • http://www.lostechies.com/members/derick.bailey/default.aspx derick.bailey

    @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.

  • Rodrigo Coutinho

    Notice that the gem Settings logic(https://github.com/binarylogic/settingslogic) does exatly that and is more dynamic