platform/lib/src/extensible.dart

32 lines
975 B
Dart
Raw Normal View History

2016-10-12 17:58:32 +00:00
final RegExp _equ = new RegExp(r'=$');
final RegExp _sym = new RegExp(r'Symbol\("([^"]+)"\)');
/// Supports accessing members of a Map as though they were actual members.
///
/// No longer requires reflection. :)
@proxy
class Extensible {
/// A set of custom properties that can be assigned to the server.
///
/// Useful for configuration and extension.
Map properties = {};
noSuchMethod(Invocation invocation) {
if (invocation.memberName != null) {
String name = _sym.firstMatch(invocation.memberName.toString()).group(1);
if (invocation.isMethod) {
return Function.apply(properties[name], invocation.positionalArguments,
invocation.namedArguments);
} else if (invocation.isGetter) {
return properties[name];
} else if (invocation.isSetter) {
return properties[name.replaceAll(_equ, '')] =
invocation.positionalArguments.first;
}
}
super.noSuchMethod(invocation);
}
}