Introducing Gimme: a lightweight ruby Registry
November 16, 2009
I recently created Gimme, a small ruby gem which allows you to configure and access a simple Registry using a nice little DSL.
Gimme in a paragraph
The general idea of Gimme is to allow a single point for configuring the well known services which your application uses (think DB connection, twitter API gateway, email server, etc). Once configured Gimme then exposes a single point to access those services, without needing to resort to singletons or global variables.Show me teh codez
Here's a simple example showing how you would configure your registry:Gimme.configure |g| g.for_the(ExternalService) do |env,name| ExternalService.new(env[:setting1],env[:setting2]) end endand here's how you'd use it from within your app:
Gimme.the(ExternalService).some_methodHere's a more complete example, showing both configuration and usage:
# your Emailer class class Emailer def initialize( smtp_hostname, port ) @smtp_hostname, @port = smtp_hostname, port puts "creating an emailer: #{inspect}" end def send_email( subject ) puts "Sending email '#{subject}' via #{inspect}" # ... end def inspect "#{@smtp_hostname}:#{@port}" end end # your Gimme configuration Gimme.configure do |g| g.for_the( Emailer ) do |env| email_settings = env[:email_settings] Emailer.new(email_settings[:hostname],email_settings[:port]) end end Gimme.environment = {:email_settings => {:hostname => 'smtp.mymailserver.com', :port => '2525'} } # in your app Gimme.the(Emailer).send_email( "Gimme is the awesomez" ) Gimme.the(Emailer).send_email( "Emailing is fun" ) Gimme.the(Emailer).send_email( "notice that only one Emailer was created above" ) Gimme.the(Emailer).send_email( "even though we sent 4 emails" )