public
Created

Fluent filter for a collection of users

  • Download Gist
ExampleUsage.as
ActionScript
1 2 3 4 5
requiredUsers = new UserDataVOFilter(allUsers)
.onlyManagers()
.withCompanyKey(3)
.withStatus(UserStatus.STAFF)
.data;
IUserDataVOFilter.as
ActionScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
package
{
public interface IUserDataVOFilter
{
function get data():Vector.<UserDataVO>;
 
function withCompanyKey(key:uint):IUserDataVOFilter;
 
function withOfficeKey(key:uint):IUserDataVOFilter;
 
function withStatus(key:uint):IUserDataVOFilter;
 
function onlyManagers():IUserDataVOFilter;
 
function onlyAdmin():IUserDataVOFilter;
 
function managersAndAdmin():IUserDataVOFilter;
 
function onlyStudents():IUserDataVOFilter;
}
}
UserDataVOFilter.as
ActionScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
package
{
public class UserDataVOFilter implements IUserDataVOFilter
{
protected var _userData:Vector.<UserDataVO>;
protected static const MANAGER_FILTER:Function = function(item:UserDataVO):Boolean {return item.isManager};
protected static const ADMIN_FILTER:Function = function(item:UserDataVO):Boolean {return item.isAdmin};
protected static const MANAGER_OR_ADMIN_FILTER:Function = function(item:UserDataVO):Boolean {return (item.isManager || item.isAdmin)};
protected static const STUDENT_FILTER:Function = function(item:UserDataVO):Boolean {return !(item.isManager || item.isAdmin)};
public function UserDataVOFilter(dataSet:Vector.<UserDataVO>)
{
_userData = dataSet;
}
public function get data():Vector.<UserDataVO>
{
return _userData;
}
 
public function withCompanyKey(key:uint):IUserDataVOFilter
{
return applyKeyFilter("companyID", key);
}
public function withOfficeKey(key:uint):IUserDataVOFilter
{
return applyKeyFilter("officeID", key);
}
public function withStatus(key:uint):IUserDataVOFilter
{
return applyKeyFilter("userStatusID", key);
}
public function onlyManagers():IUserDataVOFilter
{
return applyFilter(MANAGER_FILTER);
}
public function onlyAdmin():IUserDataVOFilter
{
return applyFilter(ADMIN_FILTER);
}
public function managersAndAdmin():IUserDataVOFilter
{
return applyFilter(MANAGER_OR_ADMIN_FILTER);
}
public function onlyStudents():IUserDataVOFilter
{
return applyFilter(STUDENT_FILTER);
}
protected function applyKeyFilter(property:String, key:uint):IUserDataVOFilter
{
var func:Function = function(item:UserDataVO, i:uint, v:Vector.<UserDataVO>):Boolean { return (item[property] == key); };
_userData = _userData.filter(func);
return this;
}
protected function applyFilter(filterFunc:Function):IUserDataVOFilter
{
var func:Function = function(item:UserDataVO, i:uint, v:Vector.<UserDataVO>):Boolean { return filterFunc(item) };
_userData = _userData.filter(func);
return this;
}
}
}
UserDataVOFilterTest.as
ActionScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
package
{
public class UserDataVOFilterTest extends TestCase {
private var instance:UserDataVOFilter;
protected var dataSet:Vector.<UserDataVO>;
 
public function UserDataVOFilterTest(methodName:String=null) {
super(methodName);
}
 
override protected function setUp():void {
super.setUp();
dataSet = new Vector.<UserDataVO>();
instance = new UserDataVOFilter(dataSet);
}
 
override protected function tearDown():void {
super.tearDown();
instance = null;
}
 
public function testInstantiated():void {
assertTrue("instance is UserDataVOFilter", instance is UserDataVOFilter);
}
 
public function test_implements_interface():void {
assertTrue("Implements interface", instance is IUserDataVOFilter);
}
 
public function testFailure():void {
assertTrue("Failing test", true);
}
public function test_filters_by_company_key():void {
dataSet.push(new UserDataVOSupport(1));
dataSet.push(new UserDataVOSupport(2));
dataSet.push(new UserDataVOSupport(3));
//from Support: var companyID:uint = userKey+2;
var filteredData:Vector.<UserDataVO> = instance.withCompanyKey(4).data;
assertEqualsVectorsIgnoringOrder('filtered by company key', Vector.<UserDataVO>([dataSet[1]]), filteredData);
}
 
public function test_filters_by_office_key():void {
dataSet.push(new UserDataVOSupport(1));
dataSet.push(new UserDataVOSupport(2));
dataSet.push(new UserDataVOSupport(3));
//from Support: var officeID:uint = userKey+1;
var filteredData:Vector.<UserDataVO> = instance.withOfficeKey(3).data;
assertEqualsVectorsIgnoringOrder('filtered by office key', Vector.<UserDataVO>([dataSet[1]]), filteredData);
}
 
public function test_filters_by_user_status_key():void {
dataSet.push(new UserDataVOSupport(1));
dataSet.push(new UserDataVOSupport(2));
dataSet.push(new UserDataVOSupport(3));
//from Support: var userStatusID:uint = userKey+1;
var filteredData:Vector.<UserDataVO> = instance.withStatus(3).data;
assertEqualsVectorsIgnoringOrder('filtered by userstatus key', Vector.<UserDataVO>([dataSet[1]]), filteredData);
}
 
public function test_filters_by_mananger():void {
dataSet.push( createManager(1) );
dataSet.push( createStudent(2) );
dataSet.push( createManager(3) );
var filteredData:Vector.<UserDataVO> = instance.onlyManagers().data;
assertEqualsVectorsIgnoringOrder('filtered by manager', Vector.<UserDataVO>([dataSet[0], dataSet[2]]), filteredData);
}
 
public function test_filters_by_admin():void {
dataSet.push( createAdmin(1) );
dataSet.push( createStudent(2) );
dataSet.push( createAdmin(3) );
var filteredData:Vector.<UserDataVO> = instance.onlyAdmin().data;
assertEqualsVectorsIgnoringOrder('filtered by admin', Vector.<UserDataVO>([dataSet[0], dataSet[2]]), filteredData);
}
public function test_filters_managers_and_admin():void {
dataSet.push( createManager(1) );
dataSet.push( createStudent(2) );
dataSet.push( createAdmin(3) );
var filteredData:Vector.<UserDataVO> = instance.managersAndAdmin().data;
assertEqualsVectorsIgnoringOrder('filtered to admin and managers', Vector.<UserDataVO>([dataSet[0], dataSet[2]]), filteredData);
}
public function test_students_only():void {
dataSet.push( createManager(1) );
dataSet.push( createStudent(2) );
dataSet.push( createAdmin(3) );
dataSet.push( createStudent(6) );
var filteredData:Vector.<UserDataVO> = instance.onlyStudents().data;
assertEqualsVectorsIgnoringOrder('filtered to admin and managers', Vector.<UserDataVO>([dataSet[1], dataSet[3]]), filteredData);
}
protected function createManager(key:uint):UserDataVO
{
var manager:UserDataVOSupport = new UserDataVOSupport(key)
manager.changeProperty('isManager', true);
return manager;
}
 
protected function createAdmin(key:uint):UserDataVO
{
var admin:UserDataVOSupport = new UserDataVOSupport(key)
admin.changeProperty('isAdmin', true);
return admin;
}
protected function createStudent(key:uint):UserDataVO
{
var student:UserDataVOSupport = new UserDataVOSupport(key);
return student;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.