platform/lib/src/extensible.dart

32 lines
936 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
2017-08-03 16:14:21 +00:00
@deprecated
2016-10-12 17:58:32 +00:00
class Extensible {
/// A set of custom properties that can be assigned to the server.
///
/// Useful for configuration and extension.
Map properties = {};
2016-10-13 20:50:27 +00:00
operator [](key) => properties[key];
operator []=(key, value) => properties[key] = value;
2016-10-12 17:58:32 +00:00
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];
}
}
super.noSuchMethod(invocation);
}
2016-11-25 23:22:33 +00:00
}