Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adds interfaces to InjectionMapping that introduce a clear funnel to …

…the mapping DSL
  • Loading branch information...
commit 453e9dc7340e0a939c6eac843ea9c9b67c38ea96 1 parent 944b338
@tschneidereit authored
View
22 src/org/swiftsuspenders/mapping/InjectionMapping.as
@@ -19,7 +19,7 @@ package org.swiftsuspenders.mapping
import org.swiftsuspenders.dependencyproviders.ValueProvider;
import org.swiftsuspenders.utils.SsInternal;
- public class InjectionMapping
+ public class InjectionMapping implements ProviderlessMapping, UnsealedMapping
{
//---------------------- Private / Protected Properties ----------------------//
private var _type : Class;
@@ -59,7 +59,7 @@ package org.swiftsuspenders.mapping
*
* @see #toSingleton()
*/
- public function asSingleton() : InjectionMapping
+ public function asSingleton() : UnsealedMapping
{
toSingleton(_type);
return this;
@@ -80,7 +80,7 @@ package org.swiftsuspenders.mapping
*
* @see #toProvider()
*/
- public function toType(type : Class) : InjectionMapping
+ public function toType(type : Class) : UnsealedMapping
{
toProvider(new ClassProvider(type));
return this;
@@ -102,7 +102,7 @@ package org.swiftsuspenders.mapping
*
* @see #toProvider()
*/
- public function toSingleton(type : Class) : InjectionMapping
+ public function toSingleton(type : Class) : UnsealedMapping
{
toProvider(new SingletonProvider(type, _creatingInjector));
return this;
@@ -122,7 +122,7 @@ package org.swiftsuspenders.mapping
*
* @see #toProvider()
*/
- public function toValue(value : Object) : InjectionMapping
+ public function toValue(value : Object) : UnsealedMapping
{
toProvider(new ValueProvider(value));
return this;
@@ -138,15 +138,15 @@ package org.swiftsuspenders.mapping
*
* @throws org.swiftsuspenders.InjectorError Sealed mappings can't be changed in any way
*/
- public function toProvider(provider : DependencyProvider) : InjectionMapping
+ public function toProvider(provider : DependencyProvider) : UnsealedMapping
{
_sealed && throwSealedError();
if (hasProvider() && provider != null && !_defaultProviderSet)
{
trace('Warning: Injector already has a mapping for ' + _mappingId + '.\n ' +
- 'If you have overridden this mapping intentionally you can use ' +
- '"injector.unmap()" prior to your replacement mapping in order to ' +
- 'avoid seeing this message.');
+ 'If you have overridden this mapping intentionally you can use ' +
+ '"injector.unmap()" prior to your replacement mapping in order to ' +
+ 'avoid seeing this message.');
_creatingInjector.hasEventListener(MappingEvent.MAPPING_OVERRIDE)
&& _creatingInjector.dispatchEvent(
new MappingEvent(MappingEvent.MAPPING_OVERRIDE, _type, _name, this));
@@ -171,7 +171,7 @@ package org.swiftsuspenders.mapping
*
* @throws org.swiftsuspenders.InjectorError Sealed mappings can't be changed in any way
*/
- public function soft() : InjectionMapping
+ public function softly() : ProviderlessMapping
{
_sealed && throwSealedError();
if (!_soft)
@@ -192,7 +192,7 @@ package org.swiftsuspenders.mapping
*
* @throws org.swiftsuspenders.InjectorError Sealed mappings can't be changed in any way
*/
- public function local() : InjectionMapping
+ public function locally() : ProviderlessMapping
{
_sealed && throwSealedError();
if (_local)
View
44 src/org/swiftsuspenders/mapping/ProviderlessMapping.as
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2012 the original author or authors
+ *
+ * Permission is hereby granted to use, modify, and distribute this file
+ * in accordance with the terms of the license agreement accompanying it.
+ */
+
+package org.swiftsuspenders.mapping
+{
+ import org.swiftsuspenders.dependencyproviders.DependencyProvider;
+
+ public interface ProviderlessMapping
+ {
+ /**
+ * @copy InjectionMapping#toType()
+ */
+ function toType(type : Class) : UnsealedMapping;
+
+ /**
+ * @copy InjectionMapping#toValue()
+ */
+ function toValue(value : Object) : UnsealedMapping;
+
+ /**
+ * @copy InjectionMapping#toSingleton()
+ */
+ function toSingleton(type : Class) : UnsealedMapping;
+
+ /**
+ * @copy InjectionMapping#asSingleton()
+ */
+ function asSingleton() : UnsealedMapping;
+
+ /**
+ * @copy InjectionMapping#toProvider()
+ */
+ function toProvider(provider : DependencyProvider) : UnsealedMapping;
+
+ /**
+ * @copy InjectionMapping#seal()
+ */
+ function seal() : Object;
+ }
+}
View
17 src/org/swiftsuspenders/mapping/UnsealedMapping.as
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2012 the original author or authors
+ *
+ * Permission is hereby granted to use, modify, and distribute this file
+ * in accordance with the terms of the license agreement accompanying it.
+ */
+
+package org.swiftsuspenders.mapping
+{
+ public interface UnsealedMapping
+ {
+ /**
+ * @copy InjectionMapping#seal()
+ */
+ function seal() : Object;
+ }
+}
View
6 test/org/swiftsuspenders/InjectionMappingTests.as
@@ -106,8 +106,8 @@ package org.swiftsuspenders
{method : 'toType', args : [Clazz]},
{method : 'toValue', args : [Clazz]},
{method : 'toProvider', args : [null]},
- {method : 'local', args : []},
- {method : 'soft', args : []}];
+ {method : 'locally', args : []},
+ {method : 'softly', args : []}];
const testedMethods : Array = [];
for each (var method : Object in methods)
{
@@ -163,7 +163,7 @@ package org.swiftsuspenders
{
const key : Object = injector.map(Interface).seal();
injector.map(Interface).unseal(key);
- injector.map(Interface).local();
+ injector.map(Interface).locally();
}
[Test]
View
18 test/org/swiftsuspenders/InjectorTests.as
@@ -594,7 +594,7 @@ package org.swiftsuspenders
[Test]
public function softMappingIsUsedIfNoParentInjectorAvailable() : void
{
- injector.map(Interface).toType(Clazz).soft();
+ injector.map(Interface).softly().toType(Clazz);
Assert.assertNotNull(injector.getInstance(Interface));
}
@@ -603,14 +603,14 @@ package org.swiftsuspenders
{
const childInjector : Injector = injector.createChildInjector();
injector.map(Interface).toType(Clazz);
- childInjector.map(Interface).toType(Clazz2).soft();
+ childInjector.map(Interface).softly().toType(Clazz2);
Assert.assertEquals(Clazz, childInjector.getInstance(Interface)['constructor']);
}
[Test]
public function localMappingsAreUsedInOwnInjector() : void
{
- injector.map(Interface).toType(Clazz).local();
+ injector.map(Interface).locally().toType(Clazz);
Assert.assertNotNull(injector.getInstance(Interface));
}
@@ -618,7 +618,7 @@ package org.swiftsuspenders
public function localMappingsArentSharedWithChildInjectors() : void
{
const childInjector : Injector = injector.createChildInjector();
- injector.map(Interface).toType(Clazz).local();
+ injector.map(Interface).locally().toType(Clazz);
childInjector.getInstance(Interface);
}
@@ -701,7 +701,7 @@ package org.swiftsuspenders
{
injector.map(Clazz);
listenToInjectorEvent(MappingEvent.PRE_MAPPING_CHANGE);
- injector.map(Clazz).soft();
+ injector.map(Clazz).softly();
assertThat(receivedInjectorEvents.pop(), equalTo(MappingEvent.PRE_MAPPING_CHANGE));
}
@@ -710,7 +710,7 @@ package org.swiftsuspenders
{
injector.map(Clazz);
listenToInjectorEvent(MappingEvent.POST_MAPPING_CHANGE);
- injector.map(Clazz).soft();
+ injector.map(Clazz).softly();
assertThat(receivedInjectorEvents.pop(), equalTo(MappingEvent.POST_MAPPING_CHANGE));
}
@@ -719,7 +719,7 @@ package org.swiftsuspenders
{
injector.map(Clazz);
listenToInjectorEvent(MappingEvent.PRE_MAPPING_CHANGE);
- injector.map(Clazz).local();
+ injector.map(Clazz).locally();
assertThat(receivedInjectorEvents.pop(), equalTo(MappingEvent.PRE_MAPPING_CHANGE));
}
@@ -728,7 +728,7 @@ package org.swiftsuspenders
{
injector.map(Clazz);
listenToInjectorEvent(MappingEvent.POST_MAPPING_CHANGE);
- injector.map(Clazz).local();
+ injector.map(Clazz).locally();
assertThat(receivedInjectorEvents.pop(), equalTo(MappingEvent.POST_MAPPING_CHANGE));
}
@@ -770,7 +770,7 @@ package org.swiftsuspenders
{
try
{
- injector.map(Clazz).local();
+ injector.map(Clazz).locally();
}
catch (error : InjectorError)
{
Please sign in to comment.
Something went wrong with that request. Please try again.