UNPKG

1.17 kBtext/coffeescriptView Raw
1
2class Binding
3 matches: (proto) ->
4 throw Error('Binding.matches not implemented')
5
6 create: (defaultFactory) ->
7 defaultFactory()
8
9class ClassBinding extends Binding
10 constructor: (@iface) ->
11 super()
12 if @iface.scope? then @inScope(@iface.scope)
13
14 to: (impl) ->
15 if @impl? then throw Error('Already bound')
16 @impl = impl
17 if impl.scope? then @scope = impl.scope.toUpperCase()
18 this
19
20 inScope: (scope) ->
21 if @scope? and @scope isnt scope
22 throw Error("Already scoped to #{@scope}")
23
24 switch scope.toUpperCase()
25 when 'SINGLETON' then @scope = 'SINGLETON'
26 when 'INSTANCE' then # Just ignore
27 else throw Error("Invalid scope, '#{scope}'")
28 this
29
30 matches: (cls) ->
31 cls is @iface
32
33class ConstantBinding extends Binding
34 constructor: (@name) ->
35
36 to: (@value) ->
37
38 matches: (cls) ->
39 cls is @name
40
41 create: ->
42 @value
43
44class Binder
45 constructor: ->
46 @bindings = []
47 @configure()
48
49 bind: (iface) ->
50 @_bind(new ClassBinding(iface))
51
52 bindConstant: (name) ->
53 @_bind(new ConstantBinding(name))
54
55 _bind: (binding) ->
56 @bindings.push(binding)
57 binding
58
59
60module.exports = Binder