0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
A simple macro for injecting dependencies
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.2
~> 1.8.4
~> 3.12
~> 2.12
 Project Readme

Collaborator

A simple gem for injecting dependencies into classes. I got bored of copying the same code again and again so I made this simple gem.

Usage

require 'collaborator'

class BugFinder
	...
end

class BugKiller
	# mixing the Collaborator module
	extend Collaborator
	
	# declare a dependency, pass an instance of the collaborator object
	dependency :bug_finder, BugFinder.new
end

The dependecy method just creates a couple of attribute accesors, so you can use the dependecy like this:

class BugKiller
	extend Collaborator 
	dependency :bug_finder, BugFinder.new

	def run
		bugs = bug_finder.run
		...
	end

end

Injecting dependencies

In your test you just inject a new dependency like so:

bug_killer = BugKiller.new
bug_killer.bug_finder = mocked_bug_finder

Lazy evaluation

You can also pass a lambda as the second argument, this will defer evaluation of the dependecy until it is actually used

class BugKiller
	extend Collaborator 
	dependency :bug_finder, ->{ BugFinder.new }

	def run
		bugs = bug_finder.run
		...
	end

end

Preparing collaborators

Sometimes it is useful to prepare the collaborators in some way before actually using them. To do this just create a method in your class called 'prepare_collaborator'.

class BugKiller
	extend Collaborator 
	dependency :bug_finder, ->{ BugFinder.new }

	# this function receives the name of the collaborator e.g. :bug_finder and the collaborator itself
	def prepare_collaborator(name, collaborator)
		collaborator.log_enable = true
	end

end

When a dependency is injected e.g. bug_killer.bug_finder = mocked_bug_finder this method is also called on the injected dependency

Copyright

Copyright (c) 2013 Sebastian. See LICENSE.txt for further details.