Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A lightweight implementation of the Registry pattern, wrapped in a pretty DSL bow.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 examples
Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 TODO
README.md

Gimme

A simple implementation of a Registry, along with a cute DSL to configure and use it. You tell Gimme how to create the well-known objects in your system. Then whenever you need an instance of one of those objects you just ask Gimme for one.

class Logger
  def initialize( threshold, name )
    puts "creating a logger called #{name} with a threshold of #{threshold}"
  end
end


Gimme.configure do |g|
  g.for_a( Logger ) do |env, name|
    name ||= 'default'
    Logger.new( env[:logging_threshold], name )
  end
end

Gimme.environment = {:logging_threshold => :INFO}


default_logger = Gimme.a(Logger)

Configuration

Use the Gimme.configure block to configure Gimme with information on how to create the well-known objects you want to be available in your registry. You can configure singletons using for_the() and Non-singletons using for_a(). Gimme will squirrel away the block supplied to those methods and then use it later on when it's asked for an instance of that object.

Use

Ask Gimme for an object by calling the Gimme.a(:some_label) retrieval method. Note that you use the same method to retrieve both singletons and non-singletons. As you'd expect, Gimme calls the creation block each time a non-singleton is requested, and only once when a singleton is requested. Subsequent requests for the singleton will simply return the object created the first time the singleton's creation block was called.

Creation parameters

If you pass additional parameters to the retrieval method then these will be forwarded on to the creation block. Note that if you are retrieving a singleton object which has already been retrieved previously then the creation parameters will be silently ignored.

Environments

You can supply Gimme with an environment hash, which generally contains application settings, For example you might add email server settings or api keys to your environment. When Gimme creates objects it passes the environment hash to the creation block. You can use that feature to configure the objects created with Gimme.

Dependency Injection/Inversion of Control

You can use Gimme as a lightweight IoC container. There is nothing to stop you calling Gimme.the() or Gimme.a() from inside a Gimme creation block. Check out the di_example.rb file in the examples directory for more inspiration.

Something went wrong with that request. Please try again.