From 14eaec763361fb338d90c0b447eb32562b9ec5e6 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Tue, 1 Jun 2021 22:50:34 +0700 Subject: [PATCH] Task: Add cubetiq dart shared library and functions and utils and extensions for developers and completed with configurable --- README.md | 28 ++++++++-- example/cubetiq_configurable_example.dart | 14 +++++ example/cubetiq_dart_shared_example.dart | 6 --- lib/configurable.dart | 7 +++ lib/cubetiq_dart_shared.dart | 8 --- lib/src/configurable/configurable.dart | 15 ++++++ .../configurable/configuration_provider.dart | 11 ++++ .../mutable_configurable_provider.dart | 19 +++++++ .../simple_configuration_provider.dart | 47 +++++++++++++++++ lib/src/configurable/system_config.dart | 52 +++++++++++++++++++ lib/src/cubetiq_dart_shared_base.dart | 6 --- pubspec.yaml | 9 ++-- test/cubetiq_configurable_test.dart | 20 +++++++ test/cubetiq_dart_shared_test.dart | 16 ------ 14 files changed, 211 insertions(+), 47 deletions(-) create mode 100644 example/cubetiq_configurable_example.dart delete mode 100644 example/cubetiq_dart_shared_example.dart create mode 100644 lib/configurable.dart delete mode 100644 lib/cubetiq_dart_shared.dart create mode 100644 lib/src/configurable/configurable.dart create mode 100644 lib/src/configurable/configuration_provider.dart create mode 100644 lib/src/configurable/mutable_configurable_provider.dart create mode 100644 lib/src/configurable/simple_configuration_provider.dart create mode 100644 lib/src/configurable/system_config.dart delete mode 100644 lib/src/cubetiq_dart_shared_base.dart create mode 100644 test/cubetiq_configurable_test.dart delete mode 100644 test/cubetiq_dart_shared_test.dart diff --git a/README.md b/README.md index 04ba68d..a8f5faf 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,37 @@ -A library for Dart developers. +# CUBETIQ Dart Shared +A minimal dart functions, utils and extensions for CUBETIQ Developers. + +# Features +- Configurable +- Text Formatter ## Usage -A simple usage example: +A simple configurable usage example: ```dart -import 'package:cubetiq_dart_shared/cubetiq_dart_shared.dart'; +import 'package:cubetiq/configurable.dart' + show + SystemConfig, + getConfigOrNull, + SimpleConfigurationProvider, + setConfig; main() { - var awesome = new Awesome(); + SystemConfig.setProvider(SimpleConfigurationProvider()); + setConfig('hello', 'Be the World!'); + + var title = getConfigOrNull('hello', defaultValue: 'Just Hello'); + print(title); //output will be: Be the World } ``` + +## Contributors +- Sambo Chea + ## Features and bugs Please file feature requests and bugs at the [issue tracker][tracker]. -[tracker]: http://example.com/issues/replaceme +[tracker]: https://git.cubetiqs.com/CUBETIQ/cubetiq_dart_shared/issues diff --git a/example/cubetiq_configurable_example.dart b/example/cubetiq_configurable_example.dart new file mode 100644 index 0000000..8bdd9d0 --- /dev/null +++ b/example/cubetiq_configurable_example.dart @@ -0,0 +1,14 @@ +import 'package:cubetiq/configurable.dart' + show + SystemConfig, + getConfigOrNull, + SimpleConfigurationProvider, + setConfig; + +void main() { + SystemConfig.setProvider(SimpleConfigurationProvider()); + setConfig('hello', 'Be the World!'); + + var title = getConfigOrNull('hello', defaultValue: 'Just Hello'); + print(title); +} diff --git a/example/cubetiq_dart_shared_example.dart b/example/cubetiq_dart_shared_example.dart deleted file mode 100644 index 7de5ad4..0000000 --- a/example/cubetiq_dart_shared_example.dart +++ /dev/null @@ -1,6 +0,0 @@ -import 'package:cubetiq_dart_shared/cubetiq_dart_shared.dart'; - -void main() { - var awesome = Awesome(); - print('awesome: ${awesome.isAwesome}'); -} diff --git a/lib/configurable.dart b/lib/configurable.dart new file mode 100644 index 0000000..9d56d6e --- /dev/null +++ b/lib/configurable.dart @@ -0,0 +1,7 @@ +library cubetiq; + +export 'src/configurable/configurable.dart'; +export 'src/configurable/configuration_provider.dart'; +export 'src/configurable/mutable_configurable_provider.dart'; +export 'src/configurable/simple_configuration_provider.dart'; +export 'src/configurable/system_config.dart'; diff --git a/lib/cubetiq_dart_shared.dart b/lib/cubetiq_dart_shared.dart deleted file mode 100644 index db364a8..0000000 --- a/lib/cubetiq_dart_shared.dart +++ /dev/null @@ -1,8 +0,0 @@ -/// Support for doing something awesome. -/// -/// More dartdocs go here. -library cubetiq_dart_shared; - -export 'src/cubetiq_dart_shared_base.dart'; - -// TODO: Export any libraries intended for clients of this package. diff --git a/lib/src/configurable/configurable.dart b/lib/src/configurable/configurable.dart new file mode 100644 index 0000000..7e7570c --- /dev/null +++ b/lib/src/configurable/configurable.dart @@ -0,0 +1,15 @@ +import 'system_config.dart'; + +/// get config or null from system config +String? getConfigOrNull(String key, {String? defaultValue}) => + SystemConfig.getOrNull(key, defaultValue: defaultValue); + +/// get config with non-null from system config +String getConfig(String key, {String? defaultValue}) => + SystemConfig.get(key, defaultValue: defaultValue); + +/// check has config key or not from system config +bool hasConfigkey(String key) => SystemConfig.containsKey(key); + +/// set config into memory +void setConfig(String key, String? value) => SystemConfig.set(key, value); \ No newline at end of file diff --git a/lib/src/configurable/configuration_provider.dart b/lib/src/configurable/configuration_provider.dart new file mode 100644 index 0000000..8b3883f --- /dev/null +++ b/lib/src/configurable/configuration_provider.dart @@ -0,0 +1,11 @@ +/// Configuration Provider +/// +/// @author sombochea +/// @since 1.0.0 +abstract class ConfigurationProvider { + /// check has key or not in implemented classes + bool containsKey(String key); + + /// get or null in implemented classes + String? getOrNull(String key, {String? defaultValue}); +} diff --git a/lib/src/configurable/mutable_configurable_provider.dart b/lib/src/configurable/mutable_configurable_provider.dart new file mode 100644 index 0000000..707a281 --- /dev/null +++ b/lib/src/configurable/mutable_configurable_provider.dart @@ -0,0 +1,19 @@ +import 'configuration_provider.dart'; + +/// Mutable Configuration Provider +/// +/// @author sombochea +/// @since 1.0.3 +abstract class MutableConfigurationProvider implements ConfigurationProvider { + /// Allow to set the value into config values + void set(String key, String? value); + + /// Allow to set all values into config values + void setAll(Map configs); + + /// Allow to remove the key from config values + void remove(String key); + + /// Allow to remove all values from config values + void removeAll(); +} diff --git a/lib/src/configurable/simple_configuration_provider.dart b/lib/src/configurable/simple_configuration_provider.dart new file mode 100644 index 0000000..769f2ca --- /dev/null +++ b/lib/src/configurable/simple_configuration_provider.dart @@ -0,0 +1,47 @@ +import 'mutable_configurable_provider.dart'; + +/// Simple Configuration Provider +/// +/// @author sombochea +/// @since 1.0.0 +class SimpleConfigurationProvider implements MutableConfigurationProvider { + final Map _configs = {}; + + @override + String? getOrNull(String key, {String? defaultValue}) { + var value = _configs[key]; + + /// if value is null, then set the default value and return it back + if (value == null) { + _configs[key] = defaultValue; + return defaultValue; + } + + return value; + } + + @override + bool containsKey(String key) { + return _configs.containsKey(key); + } + + @override + void set(String key, String? value) { + _configs[key] = value; + } + + @override + void remove(String key) { + _configs.remove(key); + } + + @override + void removeAll() { + _configs.clear(); + } + + @override + void setAll(Map configs) { + _configs.addAll(configs); + } +} diff --git a/lib/src/configurable/system_config.dart b/lib/src/configurable/system_config.dart new file mode 100644 index 0000000..2ebd241 --- /dev/null +++ b/lib/src/configurable/system_config.dart @@ -0,0 +1,52 @@ +import 'configuration_provider.dart'; +import 'mutable_configurable_provider.dart'; +import 'simple_configuration_provider.dart'; + +/// System Configuration Static Functions +/// +/// @author sombochea +/// @since 1.0.0 +class SystemConfig { + static ConfigurationProvider? _provider; + + /// Set configuration provider + static void setProvider(ConfigurationProvider provider) { + _provider = provider; + } + + static ConfigurationProvider getProvider() { + if (_provider == null) { + setProvider(SimpleConfigurationProvider()); + } + + return _provider!; + } + + static String get(String key, {String? defaultValue}) { + return getOrNull(key, defaultValue: defaultValue)!; + } + + static String? getOrNull(String key, {String? defaultValue}) { + return getProvider().getOrNull(key, defaultValue: defaultValue); + } + + static bool containsKey(String key) => getProvider().containsKey(key); + + /// Convert to mutable configuration provider + static MutableConfigurationProvider _getMutableConfigurationProvider() { + if (_provider is MutableConfigurationProvider) { + return (_provider as MutableConfigurationProvider); + } else { + throw Exception( + 'Error cannot use mutable functions without mutable configuration provider!'); + } + } + + static void set(String key, String? value) { + _getMutableConfigurationProvider().set(key, value); + } + + static void setAll(Map values) { + _getMutableConfigurationProvider().setAll(values); + } +} diff --git a/lib/src/cubetiq_dart_shared_base.dart b/lib/src/cubetiq_dart_shared_base.dart deleted file mode 100644 index e8a6f15..0000000 --- a/lib/src/cubetiq_dart_shared_base.dart +++ /dev/null @@ -1,6 +0,0 @@ -// TODO: Put public facing types in this file. - -/// Checks if you are awesome. Spoiler: you are. -class Awesome { - bool get isAwesome => true; -} diff --git a/pubspec.yaml b/pubspec.yaml index 639d985..32692a3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,14 +1,11 @@ -name: cubetiq_dart_shared -description: A starting point for Dart libraries or applications. +name: cubetiq +description: CUBETIQ Dart Shared is functions, utils, and extensions for developers. version: 1.0.0 -# homepage: https://www.example.com +homepage: https://www.cubetiqs.com environment: sdk: '>=2.12.0 <3.0.0' -# dependencies: -# path: ^1.8.0 - dev_dependencies: pedantic: ^1.10.0 test: ^1.16.0 diff --git a/test/cubetiq_configurable_test.dart b/test/cubetiq_configurable_test.dart new file mode 100644 index 0000000..0b3fc65 --- /dev/null +++ b/test/cubetiq_configurable_test.dart @@ -0,0 +1,20 @@ +import 'package:test/test.dart'; + +import 'package:cubetiq/configurable.dart'; + +void main() { + group('A simple configurable provider and functions', () { + setUp(() { + print('Register provider...'); + SystemConfig.setProvider(SimpleConfigurationProvider()); + + // Set config here + setConfig('hello', 'World'); + }); + + test('Get it back!', () { + var hello = getConfigOrNull('hello'); + expect('World', hello); + }); + }); +} diff --git a/test/cubetiq_dart_shared_test.dart b/test/cubetiq_dart_shared_test.dart deleted file mode 100644 index e1dbbbe..0000000 --- a/test/cubetiq_dart_shared_test.dart +++ /dev/null @@ -1,16 +0,0 @@ -import 'package:cubetiq_dart_shared/cubetiq_dart_shared.dart'; -import 'package:test/test.dart'; - -void main() { - group('A group of tests', () { - final awesome = Awesome(); - - setUp(() { - // Additional setup goes here. - }); - - test('First Test', () { - expect(awesome.isAwesome, isTrue); - }); - }); -}