1 module selectors.where; 2 3 import std.algorithm; 4 import std.array; 5 import std.traits; 6 7 8 import introspection.callable; 9 import introspection.attribute; 10 import introspection.parameter; 11 12 version(unittest) { 13 import fluent.asserts; 14 } 15 16 /// Filter callable lists 17 auto where(Callable[] callables) { 18 return WhereCallables(callables); 19 } 20 21 /// Filter callable lists 22 struct WhereCallables { 23 private { 24 Callable[] callables; 25 } 26 27 this(Callable[] callables) @safe pure nothrow { 28 this.callables = callables; 29 } 30 31 Callable[] anyOfAttributes(string[] attributes) @safe pure nothrow { 32 Callable[] result; 33 34 foreach (Callable callable; callables) { 35 auto callableAttributes = callable.attributes.map!"a.name".array; 36 37 if(!attributes.filter!(a => callableAttributes.canFind(a)).empty) { 38 result ~= callable; 39 } 40 } 41 42 return result; 43 } 44 } 45 46 /// Filter callables by attributes 47 unittest { 48 @("test") 49 void test() { } 50 51 enum item = describeCallable!test; 52 enum items = [ item ]; 53 54 items.where.anyOfAttributes(["other"]).length.should.equal(0); 55 items.where.anyOfAttributes(["other", "attributes"]).length.should.equal(0); 56 items.where.anyOfAttributes(["test"]).length.should.equal(1); 57 }