Task: Add cubetiq dart shared library and functions and utils and extensions for developers and completed with configurable

This commit is contained in:
Sambo Chea 2021-06-01 22:50:34 +07:00
parent c1437f133a
commit 14eaec7633
14 changed files with 211 additions and 47 deletions

View File

@ -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 <sombochea@cubetiqs.com>
## 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

View File

@ -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);
}

View File

@ -1,6 +0,0 @@
import 'package:cubetiq_dart_shared/cubetiq_dart_shared.dart';
void main() {
var awesome = Awesome();
print('awesome: ${awesome.isAwesome}');
}

7
lib/configurable.dart Normal file
View File

@ -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';

View File

@ -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.

View File

@ -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);

View File

@ -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});
}

View File

@ -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<String, String?> configs);
/// Allow to remove the key from config values
void remove(String key);
/// Allow to remove all values from config values
void removeAll();
}

View File

@ -0,0 +1,47 @@
import 'mutable_configurable_provider.dart';
/// Simple Configuration Provider
///
/// @author sombochea
/// @since 1.0.0
class SimpleConfigurationProvider implements MutableConfigurationProvider {
final Map<String, String?> _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<String, String?> configs) {
_configs.addAll(configs);
}
}

View File

@ -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<String, String?> values) {
_getMutableConfigurationProvider().setAll(values);
}
}

View File

@ -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;
}

View File

@ -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

View File

@ -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);
});
});
}

View File

@ -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);
});
});
}