Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented a first, very rough, version of a global `inject` functio…

…n that allows for defining injection points without metadata
  • Loading branch information...
commit 8af5dbdac4c6bcaed5a865dd5fee557de1858da9 1 parent 7e125f2
@tschneidereit authored
View
BIN  build/libs/hamcrest-as3-only-1.1.3.swc
Binary file not shown
View
17 src/inject.as
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2011 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
+{
+ import org.swiftsuspenders.Injector;
+
+ public function inject(type : Class, named : String = '') : *
+ {
+ var injector : Injector = Injector.currentInjector;
+ return injector.getMapping(type, named).getResponse(injector);
+ }
+}
View
20 src/org/swiftsuspenders/Injector.as
@@ -30,14 +30,16 @@ package org.swiftsuspenders
/*******************************************************************************************
* private properties *
*******************************************************************************************/
+ private static var _currentInjector : Injector;
+
private var m_parentInjector : Injector;
- private var m_applicationDomain:ApplicationDomain;
+ private var m_applicationDomain:ApplicationDomain;
private var m_mappings : Dictionary;
private var m_injectionPointLists : Dictionary;
private var m_constructorInjectionPoints : Dictionary;
private var m_attendedToInjectees : Dictionary;
private var m_xmlMetadata : XML;
-
+
/*******************************************************************************************
* public methods *
@@ -105,6 +107,9 @@ package org.swiftsuspenders
return;
}
m_attendedToInjectees[target] = true;
+
+ var previousInjector : Injector = _currentInjector;
+ _currentInjector = this;
//get injection points or cache them if this target's class wasn't encountered before
var injectionPoints : Array;
@@ -119,11 +124,14 @@ package org.swiftsuspenders
var injectionPoint : InjectionPoint = injectionPoints[i];
injectionPoint.applyInjection(target, this);
}
-
+
+ _currentInjector = previousInjector;
}
public function instantiate(clazz:Class):*
{
+ var previousInjector : Injector = _currentInjector;
+ _currentInjector = this;
var injectionPoint : InjectionPoint = m_constructorInjectionPoints[clazz];
if (!injectionPoint)
{
@@ -132,6 +140,7 @@ package org.swiftsuspenders
}
var instance : * = injectionPoint.applyInjection(clazz, this);
injectInto(instance);
+ _currentInjector = previousInjector;
return instance;
}
@@ -376,5 +385,10 @@ package org.swiftsuspenders
getInjectionPoints(Class(getDefinitionByName(parentClassName)));
injectionPoints.push.apply(injectionPoints, parentInjectionPoints);
}
+
+ public static function get currentInjector() : Injector
+ {
+ return _currentInjector;
+ }
}
}
View
49 test/org/swiftsuspenders/InjectMethodTests.as
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2011 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
+{
+ import org.hamcrest.assertThat;
+ import org.hamcrest.core.isA;
+ import org.hamcrest.object.hasProperty;
+ import org.swiftsuspenders.support.injectmethodinjectees.ClassInjectee;
+ import org.swiftsuspenders.support.types.Clazz;
+
+ public class InjectMethodTests
+ {
+ //---------------------- Public Properties ----------------------//
+
+
+ //---------------------- Private / Protected Properties ----------------------//
+ protected var injector:Injector;
+
+
+ //---------------------- Public Methods ----------------------//
+ [Before]
+ public function runBeforeEachTest():void
+ {
+ injector = new Injector();
+ }
+
+ [Test] public function injectMethodAppliesMappingFromInjectorWhenUsingInstantiate() : void
+ {
+ injector.mapClass(Clazz, Clazz);
+ var injectee : ClassInjectee = injector.instantiate(ClassInjectee);
+ assertThat(injectee, hasProperty('property', isA(Clazz)));
+ }
+
+ [Test] public function injectMethodAppliesMappingFromInjectorWhenUsingInjectInto() : void
+ {
+ injector.mapClass(Clazz, Clazz);
+ var injectee : ClassInjectee = new ClassInjectee;
+ injector.injectInto(injectee);
+ assertThat(injectee, hasProperty('property', isA(Clazz)));
+ }
+
+ //---------------------- Private / Protected Methods ----------------------//
+ }
+}
View
2  test/org/swiftsuspenders/suites/SwiftSuspendersTestSuite.as
@@ -25,6 +25,7 @@ package org.swiftsuspenders.suites
import org.swiftsuspenders.ApplicationDomainTests;
import org.swiftsuspenders.ChildInjectorTests;
import org.swiftsuspenders.GetConstructorTests;
+ import org.swiftsuspenders.InjectMethodTests;
import org.swiftsuspenders.InjectionConfigTests;
import org.swiftsuspenders.InjectorTests;
import org.swiftsuspenders.ReflectorTests;
@@ -39,6 +40,7 @@ package org.swiftsuspenders.suites
public class SwiftSuspendersTestSuite
{
public var injectorTests:InjectorTests;
+ public var injectMethodTests:InjectMethodTests;
public var propertyInjectionPointTests:PropertyInjectionPointTests;
public var reflectorTests:ReflectorTests;
public var methodInjectionPointTests:MethodInjectionPointTest;
View
18 test/org/swiftsuspenders/support/TestView.as
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2009 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.support
+{
+ import mx.core.UIComponent;
+
+ public class TestView extends UIComponent
+ {
+ public function TestView()
+ {
+ super();
+ }
+ }
+}
View
20 test/org/swiftsuspenders/support/injectmethodinjectees/ClassInjectee.as
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2011 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.support.injectmethodinjectees
+{
+ import org.swiftsuspenders.support.types.Clazz;
+
+ public class ClassInjectee
+ {
+ public var property:Clazz = inject(Clazz);
+
+ public function ClassInjectee()
+ {
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.