Tuesday, January 24, 2006

I prefer Rake to NAnt (#4)

I prefer Yaml for Ruby to the Configuration classes in the .net framework.
Configuration information is often stored in a text file. When using Rake the information can be put into a yaml file that is highly readable and maintainable without all the unnecessary noise.
--- !ruby/object:Configuration 
catalog: cat
data_source: source
password: password
user: jay
Next, you will need to create a Configuration class. Yaml.rb::load will automatically load your configuration information into an instance of the Configuration class.
class Configuration
attr_accessor :user, :password, :data_source, :catalog
end
At this point you are ready to access your configuration information via methods of the object. The following test verifies the expected behavior.
require 'yaml'
require 'test/unit'

class TestConfiguration < Test::Unit::TestCase
def testYmlLoaded
config = YAML::load(File.open('config.yaml'))
assert_equal("jay",config.user)
assert_equal("password",config.password)
assert_equal("source",config.data_source)
assert_equal("cat",config.catalog)
end
end
*SciTE formatting

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.