Docunext


Ruby YAML and Nesta

January 30th, 2010

I'm hacking up my forked version of Nesta and right now I'm wrestling with YAML.

In particular, I've never seen this syntax:

    def self.google_analytics_code
      get(environment)["google_analytics_code"]
    end

I get the idea - its setting an object variable based upon the runtime environment (development, production, test). Its the parentheses and the square brackets that confuse me. I'm used to functions and methods getting parameters inside of parentheses, not both parentheses and square brackets.

The get method for the Configuration class is this:

      def self.get(key, default = {})
        configuration[key].nil? ? default : configuration[key]
      end

Where does the environment fit in? I've searched for Ruby YAML configuration environment without luck, so now I'm searching for private ruby class methods.

Orion Edwards has made interesting comment on StackOverflow.com about []:

There is also a convention that it is used as a class method in the same way you might use a static Create method in C# or Java.

Interesting, I recall now that [] is actually a method in itself. Its interesting, but not a solution for my problem.

My goal is the get a hash object, not a string value for a key. If the key does not exist, I'd like to return a default hash object.

This appears to be the best explanation of whats up:

http://www.ruby-forum.com/topic/135931

Actually, that is from 2008 and seems a bit-outdated.

OK, I think I figured it out. Its times like these when I disagree with the purported advantages of YAML. This is what I ended up with:

    def self.cache
      #configuration[self.environment]["cache"].nil? ? ['cache_enabled' => false] : configuration[self.environment]["cache"]
      #get((environment)["cache"], { 'cache_enabled' => false })
      conf.fetch(self.environment, conf).fetch('cache', { 'cache' => { 'cache_enabled' => false } })
    end

And cleaned up:

    def self.cache
      conf.fetch(self.environment, conf).fetch('cache', { 'cache' => { 'cache_enabled' => false } })
    end

Hmmmm. Seems a bit complicated, but not bad.

Yearly Indexes: 2003 2004 2006 2007 2008 2009 2010 2011 2012 2013 2015 2019 2020 2022