Tim Perrett
2007-Dec-30 23:34 UTC
Loading config YAML into merb process for the life of the process
Hey Chaps, Forgive me for not fully understanding the inner workings of merb, but I would like to read in a YAML configuration file once when the application is booted rather than reading it every time it is used (as presumably the overhead in doing that is significant?) - I had presumed this is how both Merb and rails do it for there database.yml config files (hence its a requirement to restart the process if you change the DB config) but after looking at the rails trunk there seems to be nothing obvious going on that would cache the returned configuration. Are there hooks available so that gem plugins for merb can run code when the application is at different parts of booting? If so, how on earth would I then store said configuration? Many thanks for any pointers Tim
Ezra Zygmuntowicz
2007-Dec-31 00:12 UTC
Loading config YAML into merb process for the life of the process
On Dec 30, 2007, at 3:34 PM, Tim Perrett wrote:> Hey Chaps, > > Forgive me for not fully understanding the inner workings of merb, but > I would like to read in a YAML configuration file once when the > application is booted rather than reading it every time it is used (as > presumably the overhead in doing that is significant?) - I had > presumed this is how both Merb and rails do it for there database.yml > config files (hence its a requirement to restart the process if you > change the DB config) but after looking at the rails trunk there seems > to be nothing obvious going on that would cache the returned > configuration. > > Are there hooks available so that gem plugins for merb can run code > when the application is at different parts of booting? If so, how on > earth would I then store said configuration? > > Many thanks for any pointers > > TimHey Tim- There are a few ways of doing this. The best way it to use the Merb::Plugins.config hash. If you generate a fresh plugin: merb -P foobar Then look in the plugin at the lib/foobar.rb file you will see this: # make sure we''re running inside Merb if defined?(Merb::Plugins) # Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it Merb::Plugins.config[:foobar] = { :chickens => false } Merb::Plugins.add_rakefiles "foobar/merbtasks" end So your best bet is to load the yaml file here and store it away in the Merb::Plugins.config hash.. Something like this: # Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it Merb::Plugins.config[:foobar] = Erubis.load_yaml_file(MERB_ROOT / ''config'' / ''foobar.yml'' ) Then you can put your foobar.yml file inside of MERB_ROOT/config/ And now in your app when you want to refer to the hash of stuff loaded from the yaml file you woudl access it like this: Merb::Plugins.config[:foobar][:somekey] Doing it will Erubis.load_yaml_file will load the yaml file and interpolate any erb tags inside of it before yaml loading it. Make sense? Cheers- - Ezra Zygmuntowicz -- Founder & Software Architect -- ezra at engineyard.com -- EngineYard.com
Tim Perrett
2008-Jan-02 10:25 UTC
Loading config YAML into merb process for the life of the process
Hey Ezra, Thanks, thats brilliant :) Cheers Tim On 31 Dec 2007, at 00:12, Ezra Zygmuntowicz wrote:> Hey Tim- > > There are a few ways of doing this. The best way it to use the > Merb::Plugins.config hash. If you generate a fresh plugin: > > merb -P foobar > > Then look in the plugin at the lib/foobar.rb file you will see this: > > # make sure we''re running inside Merb > if defined?(Merb::Plugins) > > # Merb gives you a Merb::Plugins.config hash...feel free to put > your stuff in your piece of it > Merb::Plugins.config[:foobar] = { > :chickens => false > } > > Merb::Plugins.add_rakefiles "foobar/merbtasks" > end > > > So your best bet is to load the yaml file here and store it away in > the Merb::Plugins.config hash.. Something like this: > > > # Merb gives you a Merb::Plugins.config hash...feel free to put > your stuff in your piece of it > Merb::Plugins.config[:foobar] = Erubis.load_yaml_file(MERB_ROOT / > ''config'' / ''foobar.yml'' ) > > > Then you can put your foobar.yml file inside of MERB_ROOT/config/ > And now in your app when you want to refer to the hash of stuff > loaded from the yaml file you woudl access it like this: > > Merb::Plugins.config[:foobar][:somekey] > > > Doing it will Erubis.load_yaml_file will load the yaml file and > interpolate any erb tags inside of it before yaml loading it. > > Make sense? > > Cheers- > > - Ezra Zygmuntowicz