platform/lib/src/core/anonymous_service.dart
2018-07-12 11:19:25 -04:00

53 lines
1.6 KiB
Dart

import 'dart:async';
import 'service.dart';
/// An easy helper class to create one-off services without having to create an entire class.
///
/// Well-suited for testing.
class AnonymousService extends Service {
FutureOr Function([Map]) _index;
FutureOr Function(Object, [Map]) _read, _create, _remove;
FutureOr Function(Object, Object, [Map]) _modify, _update;
AnonymousService(
{FutureOr index([Map params]),
FutureOr read(id, [Map params]),
FutureOr create(data, [Map params]),
FutureOr modify(id, data, [Map params]),
FutureOr update(id, data, [Map params]),
FutureOr remove(id, [Map params])})
: super() {
_index = index;
_read = read;
_create = create;
_modify = modify;
_update = update;
_remove = remove;
}
@override
index([Map params]) => new Future.sync(
() => _index != null ? _index(params) : super.index(params));
@override
read(id, [Map params]) => new Future.sync(
() => _read != null ? _read(id, params) : super.read(id, params));
@override
create(data, [Map params]) => new Future.sync(() =>
_create != null ? _create(data, params) : super.create(data, params));
@override
modify(id, data, [Map params]) => new Future.sync(() => _modify != null
? _modify(id, data, params)
: super.modify(id, data, params));
@override
update(id, data, [Map params]) => new Future.sync(() => _update != null
? _update(id, data, params)
: super.update(id, data, params));
@override
remove(id, [Map params]) => new Future.sync(
() => _remove != null ? _remove(id, params) : super.remove(id, params));
}