From c44abaaedb71cf29004263aaa694fc35aa59dada Mon Sep 17 00:00:00 2001 From: Sergey Penkovsky Date: Wed, 20 Oct 2021 10:17:48 +0300 Subject: [PATCH] added experimental api --- lib/src/helper.dart | 69 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/lib/src/helper.dart b/lib/src/helper.dart index cac39f7..3295c96 100644 --- a/lib/src/helper.dart +++ b/lib/src/helper.dart @@ -11,6 +11,7 @@ /// limitations under the License. /// import 'package:cherrypick/src/scope.dart'; +import 'package:meta/meta.dart'; Scope? _rootScope; @@ -33,4 +34,72 @@ class CherryPick { _rootScope = null; } } + + /// RU: Метод открывает дочерний [Scope]. + /// ENG: The method open the child [Scope]. + /// + /// Дочерний [Scope] открывается с [scopeName] + /// Child [Scope] open with [scopeName] + /// + /// Example: + /// ``` + /// final String scopeName = 'firstScope.secondScope'; + /// final subScope = CherryPick.openScope(scopeName); + /// ``` + /// + /// + @experimental + static Scope openScope({String scopeName = '', String separator = '.'}) { + if (scopeName.isEmpty) { + return openRootScope(); + } + + final nameParts = scopeName.split(separator); + if (nameParts.isEmpty) { + throw Exception('Can not open sub scope because scopeName can not split'); + } + + return nameParts.fold( + openRootScope(), + (Scope previousValue, String element) => + previousValue.openSubScope(element)); + } + + /// RU: Метод открывает дочерний [Scope]. + /// ENG: The method open the child [Scope]. + /// + /// Дочерний [Scope] открывается с [scopeName] + /// Child [Scope] open with [scopeName] + /// + /// Example: + /// ``` + /// final String scopeName = 'firstScope.secondScope'; + /// final subScope = CherryPick.closeScope(scopeName); + /// ``` + /// + /// + @experimental + static void closeScope({String scopeName = '', String separator = '.'}) { + if (scopeName.isEmpty) { + closeRootScope(); + } + + final nameParts = scopeName.split(separator); + if (nameParts.isEmpty) { + throw Exception( + 'Can not close sub scope because scopeName can not split'); + } + + if (nameParts.length > 1) { + final lastPart = nameParts.removeLast(); + + final scope = nameParts.fold( + openRootScope(), + (Scope previousValue, String element) => + previousValue.openSubScope(element)); + scope.closeSubScope(lastPart); + } else { + openRootScope().closeSubScope(nameParts[0]); + } + } }