From 0144ebe34e8480ca7906f7836d7fdaa2b3313bdc Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 10 Apr 2019 15:20:02 -0400 Subject: [PATCH] hasNamed --- angel_container/CHANGELOG.md | 3 +++ angel_container/lib/src/container.dart | 16 ++++++++++++++++ angel_container/pubspec.yaml | 2 +- angel_container/test/has_test.dart | 8 ++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/angel_container/CHANGELOG.md b/angel_container/CHANGELOG.md index 8fd3c63d..87af614f 100644 --- a/angel_container/CHANGELOG.md +++ b/angel_container/CHANGELOG.md @@ -1,3 +1,6 @@ +# 1.0.1 +* Added `hasNamed`. + # 1.0.0 * Removed `@GenerateReflector`. diff --git a/angel_container/lib/src/container.dart b/angel_container/lib/src/container.dart index 63c24493..a5b35332 100644 --- a/angel_container/lib/src/container.dart +++ b/angel_container/lib/src/container.dart @@ -21,6 +21,7 @@ class Container { return new Container._child(this); } + /// Determines if the container has an injection of the given type. bool has([Type t]) { var search = this; t ??= T == dynamic ? t : T; @@ -38,6 +39,21 @@ class Container { return false; } + /// Determines if the container has a named singleton with the given [name]. + bool hasNamed(String name) { + var search = this; + + while (search != null) { + if (search._namedSingletons.containsKey(name)) { + return true; + } else { + search = search._parent; + } + } + + return false; + } + /// Instantiates an instance of [T]. /// /// In contexts where a static generic type cannot be used, use diff --git a/angel_container/pubspec.yaml b/angel_container/pubspec.yaml index ff82fcd0..e06a4572 100644 --- a/angel_container/pubspec.yaml +++ b/angel_container/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_container -version: 1.0.0 +version: 1.0.1 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 index 0a831c19..309c6ce7 100644 --- a/angel_container/test/has_test.dart +++ b/angel_container/test/has_test.dart @@ -7,6 +7,7 @@ void main() { setUp(() { container = new Container(const EmptyReflector()) ..registerSingleton(new Song(title: 'I Wish')) + ..registerNamedSingleton('foo', 1) ..registerFactory((container) { return new Artist( name: 'Stevie Wonder', @@ -15,6 +16,13 @@ void main() { }); }); + test('hasNamed', () { + var child = container.createChild()..registerNamedSingleton('bar', 2); + expect(child.hasNamed('foo'), true); + expect(child.hasNamed('bar'), true); + expect(child.hasNamed('baz'), false); + }); + test('has on singleton', () { expect(container.has(), true); });