Skip to content

Commit

Permalink
first sketch for using execute-tags
Browse files Browse the repository at this point in the history
  • Loading branch information
creynders committed Apr 14, 2013
1 parent 2181f6d commit 5cca526
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//------------------------------------------------------------------------------
// Copyright (c) 2009-2013 the original author or authors. All Rights Reserved.
//
// NOTICE: You are permitted to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//------------------------------------------------------------------------------

package robotlegs.bender.extensions.payloadEvents.api
{

public class ExecutionReflectorError extends Error
{

/*============================================================================*/
/* Constructor */
/*============================================================================*/

public function ExecutionReflectorError(message:* = "", id:* = 0)
{
super(message, id);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//------------------------------------------------------------------------------
// Copyright (c) 2009-2013 the original author or authors. All Rights Reserved.
//
// NOTICE: You are permitted to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//------------------------------------------------------------------------------

package robotlegs.bender.extensions.payloadEvents.impl
{
import robotlegs.bender.extensions.commandCenter.api.ICommandMapping;
import robotlegs.bender.extensions.commandCenter.api.ICommandMappingList;
import robotlegs.bender.extensions.commandCenter.dsl.ICommandConfigurator;
import robotlegs.bender.extensions.commandCenter.dsl.ICommandMapper;
import robotlegs.bender.extensions.commandCenter.dsl.ICommandUnmapper;
import robotlegs.bender.extensions.commandCenter.impl.CommandMapping;
import robotlegs.bender.extensions.payloadEvents.impl.execution.ExecutionReflector;

public class PayloadEventCommandMapper implements ICommandMapper, ICommandUnmapper, ICommandConfigurator
{

/*============================================================================*/
/* Private Properties */
/*============================================================================*/

private var _mappings:ICommandMappingList;

private var _mapping:ICommandMapping;

private var _executeReflector:ExecutionReflector;

/*============================================================================*/
/* Constructor */
/*============================================================================*/

public function PayloadEventCommandMapper(mappings : ICommandMappingList)
{
_mappings = mappings;
_executeReflector = new ExecutionReflector();
}

/*============================================================================*/
/* Public Functions */
/*============================================================================*/

public function toCommand(commandClass:Class):ICommandConfigurator
{
_mapping = new CommandMapping(commandClass);
const executeMethod:String = _executeReflector.describeExecutionMethodForClass(commandClass);
executeMethod && _mapping.setExecuteMethod(executeMethod);
_mappings.addMapping(_mapping);
return this;
}

/**
* @inheritDoc
*/
public function fromCommand(commandClass:Class):void
{
_mappings.removeMappingFor(commandClass);
}

/**
* @inheritDoc
*/
public function fromAll():void
{
_mappings.removeAllMappings();
}

/**
* @inheritDoc
*/
public function once(value:Boolean = true):ICommandConfigurator
{
_mapping.setFireOnce(value);
return this;
}

/**
* @inheritDoc
*/
public function withGuards(... guards):ICommandConfigurator
{
_mapping.addGuards.apply(null, guards);
return this;
}

/**
* @inheritDoc
*/
public function withHooks(... hooks):ICommandConfigurator
{
_mapping.addHooks.apply(null, hooks);
return this;
}

/**
* @inheritDoc
*/
public function withExecuteMethod(name:String):ICommandConfigurator
{
_mapping.setExecuteMethod(name);
return this;
}

/**
* @inheritDoc
*/
public function withPayloadInjection(value:Boolean = true):ICommandConfigurator
{
_mapping.setPayloadInjectionEnabled(value);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ package robotlegs.bender.extensions.payloadEvents.impl
*/
import flash.events.Event;
import flash.events.IEventDispatcher;

import org.swiftsuspenders.Injector;

import robotlegs.bender.extensions.commandCenter.api.ICommandExecutor;
import robotlegs.bender.extensions.commandCenter.api.ICommandMappingList;
import robotlegs.bender.extensions.commandCenter.api.ICommandTrigger;
Expand All @@ -21,9 +23,9 @@ package robotlegs.bender.extensions.payloadEvents.impl
import robotlegs.bender.extensions.commandCenter.impl.CommandMappingList;
import robotlegs.bender.extensions.commandCenter.impl.CommandPayload;
import robotlegs.bender.extensions.payloadEvents.api.IPayloadExtractionPoint;
import robotlegs.bender.framework.api.ILogger;
import robotlegs.bender.extensions.payloadEvents.impl.extraction.PayloadExtractionDescription;
import robotlegs.bender.extensions.payloadEvents.impl.extraction.PayloadReflector;
import robotlegs.bender.framework.api.ILogger;

public class PayloadEventCommandTrigger implements ICommandTrigger
{
Expand Down Expand Up @@ -74,9 +76,9 @@ package robotlegs.bender.extensions.payloadEvents.impl
/* Public Functions */
/*============================================================================*/

public function createMapper():CommandMapper
public function createMapper():PayloadEventCommandMapper
{
return new CommandMapper(_mappings);
return new PayloadEventCommandMapper(_mappings);
}

public function activate():void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//------------------------------------------------------------------------------
// Copyright (c) 2009-2013 the original author or authors. All Rights Reserved.
//
// NOTICE: You are permitted to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//------------------------------------------------------------------------------

package robotlegs.bender.extensions.payloadEvents.impl.execution
{
import flash.utils.describeType;

import robotlegs.bender.extensions.payloadEvents.api.ExecutionReflectorError;

public class ExecutionReflector
{

/*============================================================================*/
/* Public Functions */
/*============================================================================*/

public function describeExecutionMethodForClass(type:Class):String
{
var factoryDescription:XML = describeType(type).factory[0];
var list:XMLList = factoryDescription.method.metadata.(@name == 'Execute');
switch (list.length())
{
case 1:
var memberDescription:XML = list[0].parent();
return memberDescription.attribute('name');
case 0:
return null;
default:
throw new ExecutionReflectorError('Only one Execute-tagged method allowed');
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

package robotlegs.bender.extensions.payloadEvents
{
import robotlegs.bender.extensions.payloadEvents.impl.extraction.FieldPayloadExtractionPointTest;
import robotlegs.bender.extensions.payloadEvents.impl.extraction.MethodPayloadExtractionPointTest;
import robotlegs.bender.extensions.payloadEvents.impl.PayloadEventCommandMapIntegrationTest;
import robotlegs.bender.extensions.payloadEvents.impl.PayloadEventCommandTriggerTest;
import robotlegs.bender.extensions.payloadEvents.impl.extraction.PayloadExtractorTest;
import robotlegs.bender.extensions.payloadEvents.impl.execution.ExecutionReflectorTest;
import robotlegs.bender.extensions.payloadEvents.impl.extraction.FieldPayloadExtractionPointTest;
import robotlegs.bender.extensions.payloadEvents.impl.extraction.MethodPayloadExtractionPointTest;
import robotlegs.bender.extensions.payloadEvents.impl.extraction.PayloadReflectorTest;

[RunWith("org.flexunit.runners.Suite")]
[Suite]
Expand All @@ -28,10 +29,12 @@ package robotlegs.bender.extensions.payloadEvents

public var payloadEventsExtensionIntegration:PayloadEventCommandMapIntegrationTest;

public var payloadExtractor:PayloadExtractorTest;
public var payloadExtractor:PayloadReflectorTest;

public var fieldPayloadExtractionPoint : FieldPayloadExtractionPointTest;

public var methodPayloadExtractionPoint : MethodPayloadExtractionPointTest;

public var executionReflector : ExecutionReflectorTest;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package robotlegs.bender.extensions.payloadEvents.impl
import robotlegs.bender.extensions.commandCenter.dsl.ICommandMapper;
import robotlegs.bender.extensions.commandCenter.dsl.ICommandUnmapper;
import robotlegs.bender.extensions.payloadEvents.api.IPayloadEventCommandMap;
import robotlegs.bender.extensions.payloadEvents.support.ClassReportingCommand;
import robotlegs.bender.extensions.payloadEvents.support.ExecuteTaggedClassReportingMethodCommand;
import robotlegs.bender.extensions.payloadEvents.support.EventReportingCommand;
import robotlegs.bender.extensions.payloadEvents.support.ExecuteWithParametersCommand;
import robotlegs.bender.extensions.payloadEvents.support.InjectionPointsCommand;
Expand Down Expand Up @@ -83,11 +83,11 @@ package robotlegs.bender.extensions.payloadEvents.impl

[Test]
public function commands_are_executed() : void{
subject.map(SupportEvent.TYPE).toCommand(ClassReportingCommand);
subject.map(SupportEvent.TYPE).toCommand(ExecuteTaggedClassReportingMethodCommand);

dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE));

assertThat(reported,array(ClassReportingCommand));
assertThat(reported,array(ExecuteTaggedClassReportingMethodCommand));
}

[Test]
Expand All @@ -113,7 +113,7 @@ package robotlegs.bender.extensions.payloadEvents.impl
}

[Test]
public function test_event_is_injected_when_no_payload_dispatched() : void{
public function event_is_injected_when_no_payload_dispatched() : void{
subject.map(SupportEvent.TYPE).toCommand(EventReportingCommand);
var event:Event = new SupportEvent(SupportEvent.TYPE);

Expand All @@ -123,8 +123,8 @@ package robotlegs.bender.extensions.payloadEvents.impl
}

[Test]
public function test_command_does_not_execute_when_incorrect_eventClass_dispatched() : void{
subject.map(SupportEvent.TYPE,Event).toCommand(ClassReportingCommand);
public function command_does_not_execute_when_incorrect_eventClass_dispatched() : void{
subject.map(SupportEvent.TYPE,Event).toCommand(ExecuteTaggedClassReportingMethodCommand);

dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package robotlegs.bender.extensions.payloadEvents.impl
import org.swiftsuspenders.Injector;

import robotlegs.bender.extensions.commandCenter.impl.CommandMapper;
import robotlegs.bender.extensions.payloadEvents.impl.extraction.PayloadReflector;
import robotlegs.bender.extensions.payloadEvents.support.OrderedExtractionPointsEvent;
import robotlegs.bender.framework.api.ILogger;
import robotlegs.bender.extensions.payloadEvents.impl.extraction.PayloadReflector;

public class PayloadEventCommandTriggerTest
{
Expand Down Expand Up @@ -72,7 +72,7 @@ package robotlegs.bender.extensions.payloadEvents.impl
{
var actual:Object = subject.createMapper();

assertThat(actual, instanceOf(CommandMapper));
assertThat(actual, instanceOf(PayloadEventCommandMapper));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package robotlegs.bender.extensions.payloadEvents.impl.execution
{
import org.hamcrest.assertThat;
import org.hamcrest.object.equalTo;
import org.hamcrest.object.nullValue;

import robotlegs.bender.extensions.payloadEvents.support.ExecuteTaggedCommand;
import robotlegs.bender.extensions.payloadEvents.support.MultipleExecuteTaggedMethodsCommand;

public class ExecutionReflectorTest{
private var subject : ExecutionReflector;

[Before]
public function setup() : void{
subject = new ExecutionReflector();
}

[Test]
public function describes_execution_method_as_null_when_tag_not_found() : void{
assertThat(subject.describeExecutionMethodForClass(Class),nullValue());
}

[Test]
public function desscribes_execution_method_when_tag_found() : void{
assertThat(subject.describeExecutionMethodForClass(ExecuteTaggedCommand),equalTo('executeTaggedMethod'));
}
[Test(expects="robotlegs.bender.extensions.payloadEvents.api.ExecutionReflectorError")]
public function error_is_thrown_when_multiple_execute_tags_found() : void{
subject.describeExecutionMethodForClass(MultipleExecuteTaggedMethodsCommand);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

package robotlegs.bender.extensions.payloadEvents.support
{
import robotlegs.bender.extensions.commandCenter.api.ICommand;

public class ClassReportingCommand implements ICommand
public class ExecuteTaggedClassReportingMethodCommand
{

/*============================================================================*/
Expand All @@ -23,9 +22,10 @@ package robotlegs.bender.extensions.payloadEvents.support
/* Public Functions */
/*============================================================================*/

public function execute():void
[Execute]
public function report():void
{
reportingFunc(ClassReportingCommand);
reportingFunc(ExecuteTaggedClassReportingMethodCommand);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//------------------------------------------------------------------------------
// Copyright (c) 2009-2013 the original author or authors. All Rights Reserved.
//
// NOTICE: You are permitted to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//------------------------------------------------------------------------------

package robotlegs.bender.extensions.payloadEvents.support
{

public class ExecuteTaggedCommand
{

/*============================================================================*/
/* Public Functions */
/*============================================================================*/

[Execute]
public function executeTaggedMethod():void
{

}
}
}
Loading

0 comments on commit 5cca526

Please sign in to comment.