From 4500f33ae9eaf8756879ffe586c22afda61ab428 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 26 Aug 2018 18:55:21 -0400 Subject: [PATCH] add Container.has --- angel_container/CHANGELOG.md | 3 ++ angel_container/lib/src/container.dart | 31 ++++++++++++------- angel_container/pubspec.yaml | 2 +- angel_container/test/has_test.dart | 42 ++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 angel_container/test/has_test.dart diff --git a/angel_container/CHANGELOG.md b/angel_container/CHANGELOG.md index 08bf944b..c0458ccf 100644 --- a/angel_container/CHANGELOG.md +++ b/angel_container/CHANGELOG.md @@ -1,3 +1,6 @@ +# 1.0.0-alpha.9 +* Added `Container.has`. + # 1.0.0-alpha.8 * Fixed a bug where `_ReflectedTypeInstance.isAssignableTo` always failed. * Added `@GenerateReflector` annotation. diff --git a/angel_container/lib/src/container.dart b/angel_container/lib/src/container.dart index 19844b0e..38ecf942 100644 --- a/angel_container/lib/src/container.dart +++ b/angel_container/lib/src/container.dart @@ -20,6 +20,23 @@ class Container { return new Container._child(this); } + bool has([Type t]) { + var search = this; + t ??= T == dynamic ? t : T; + + while (search != null) { + if (search._singletons.containsKey(t)) { + return true; + } else if (search._factories.containsKey(t)) { + return true; + } else { + search = search._parent; + } + } + + return false; + } + /// Instantiates an instance of [T]. /// /// In contexts where a static generic type cannot be used, use @@ -27,22 +44,14 @@ class Container { T make([Type type]) { type ??= T; - // Find a singleton, if any. var search = this; while (search != null) { if (search._singletons.containsKey(type)) { + // Find a singleton, if any. return search._singletons[type] as T; - } else { - search = search._parent; - } - } - - // Find a factory, if any. - search = this; - - while (search != null) { - if (search._factories.containsKey(type)) { + } else if (search._factories.containsKey(type)) { + // Find a factory, if any. return search._factories[type](this) as T; } else { search = search._parent; diff --git a/angel_container/pubspec.yaml b/angel_container/pubspec.yaml index a1417fe2..e9717ef1 100644 --- a/angel_container/pubspec.yaml +++ b/angel_container/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_container -version: 1.0.0-alpha.8 +version: 1.0.0-alpha.9 author: Tobe O description: "A better IoC container and dependency injector for Angel, ultimately allowing Angel to be used without dart:mirrors." homepage: https://github.com/angel-dart/container.git diff --git a/angel_container/test/has_test.dart b/angel_container/test/has_test.dart new file mode 100644 index 00000000..0a831c19 --- /dev/null +++ b/angel_container/test/has_test.dart @@ -0,0 +1,42 @@ +import 'package:angel_container/angel_container.dart'; +import 'package:test/test.dart'; + +void main() { + Container container; + + setUp(() { + container = new Container(const EmptyReflector()) + ..registerSingleton(new Song(title: 'I Wish')) + ..registerFactory((container) { + return new Artist( + name: 'Stevie Wonder', + song: container.make(), + ); + }); + }); + + test('has on singleton', () { + expect(container.has(), true); + }); + + test('has on factory', () { + expect(container.has(), true); + }); + + test('false if neither', () { + expect(container.has(), false); + }); +} + +class Artist { + final String name; + final Song song; + + Artist({this.name, this.song}); +} + +class Song { + final String title; + + Song({this.title}); +}