This is a small utility component that you can use in your applications to ease the management of dependencies between objects. The idea is simple, you have a factory object (we'll call this the injector) where you define some mappings. Each mapping has a unique id defined by you. From different modules you can query the injector to give you a new instance of a specific mapping. Within classes you can define dependecies which will be satisfied on creation of a new instance of that specific class. It can be used in both browser and nodejs apps.

Installation

npm install coffee-sweetener

Why Coffee Sweetener?

Some people like coffee as it is, others like it with sugar. Same it's true for components like this one, some people like them others can live without them. But not be afraid of trying it as, not like sugar, it will keep your applications slim.

Examples

Get Instances

# define a class
class MyClass
    sayYeah: ->
        console.log "YEAH!"

# get the Injector
CoffeeSweetener = require "coffee-sweetener"
Injector = new CoffeeSweetener()

# map MyClass in the Injector
Injector.map
    klass: MyClass

# ask the Injector to give you a new instance of MyClass
instance = Injector.getInstanceOf "MyClass"

# use the instance
instance.sayYeah() # this prints "YEAH!" to the console

Use Injection Points

# define a class
class Wheels
    printOut: ->
        console.log "Cool Wheels"

# define a class with injection points
class Car
    inject:
        wheels: "Wheels"

    describe: ->
        console.log "This car has some #{@wheels.printOut}"

# get the Injector
CoffeeSweetener = require "coffee-sweetener"
Injector = new CoffeeSweetener()

# map Wheels in the Injector
Injector.map
    klass: Wheels

# map Car in the Injector
Injector.map
    klass: Car

# ask the Injector to give you a new instance of Car
myCar = Injector.getInstanceOf "Car"

# use the instance
myCar.describe() # this prints "This car has some Cool Wheels" to the console

For the full API please go to the official repo HERE

The API is inspired by SwiftSuspenders which I used while developing Flashy things.