Skip to content

Instantly share code, notes, and snippets.

@Stray
Created July 5, 2011 11:39
Show Gist options
  • Save Stray/1064691 to your computer and use it in GitHub Desktop.
Save Stray/1064691 to your computer and use it in GitHub Desktop.
Fluent filter for a collection of users
requiredUsers = new UserDataVOFilter(allUsers)
.onlyManagers()
.withCompanyKey(3)
.withStatus(UserStatus.STAFF)
.data;
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;
}
}
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;
}
}
}
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment