General

Gems

Web Based

Deject

Dependency Injection

Hard dependencies kick ass. They make your code clear and easy to understand. But, of course, they're hard, you can't change them (or can't reasonably change them). So when you go to test, it sucks. When you want to reuse, it sucks. How to get around this? Inject your dependencies.

And while it's not the worst thing in the world to do custom dependency injection in Ruby, it still gets obnoxious.

Deject attempts to make this easier.

require 'deject'

class SomeClass
  Deject self
  dependency(:some_dependency) { |instance| 'default' }
end

# uses the default
SomeClass.new.some_dependency                 # => "default"

# override the default
SomeClass.new
         .with_some_dependency('new default')
         .some_dependency                     # => "new default"

Deject also supports global registration so that default implementations do not need to be provided, and can take a block anywhere a value is provided so that values can be determined dynamically.

See github page for more examples of what it can do.

I've implemented this lib four or five ways, and in the end, resorted to a very curious style of "functional" programming, where everything is done with closures and local variables. I just found this was the easiest to add new functionality to.