Inject config
This commit is contained in:
parent
c7b7612f65
commit
eb38fa232d
5 changed files with 38 additions and 1 deletions
1
.travis.yml
Normal file
1
.travis.yml
Normal file
|
@ -0,0 +1 @@
|
|||
language: dart
|
|
@ -1,4 +1,8 @@
|
|||
# Angel Configuration
|
||||
|
||||
![version 1.0.1+6](https://img.shields.io/badge/version-1.0.1+6-red.svg)
|
||||
![build status](https://travis-ci.org/angel-dart/configuration.svg)
|
||||
|
||||
Isomorphic YAML configuration loader for Angel.
|
||||
|
||||
# About
|
||||
|
@ -28,6 +32,10 @@ import 'package:angel_configuration/angel_configuration.dart';
|
|||
main() async {
|
||||
Angel angel = new Angel();
|
||||
angel.configure(loadConfigurationFile()); // It's that easy!
|
||||
|
||||
app.get('/foo', (Configuration config) {
|
||||
return config.some_key;
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -2,8 +2,35 @@ library angel_configuration;
|
|||
|
||||
import 'dart:io';
|
||||
import 'package:angel_framework/angel_framework.dart';
|
||||
import 'package:angel_route/src/extensible.dart';
|
||||
import 'package:yaml/yaml.dart';
|
||||
|
||||
final RegExp _equ = new RegExp(r'=$');
|
||||
final RegExp _sym = new RegExp(r'Symbol\("([^"]+)"\)');
|
||||
|
||||
class Configuration {
|
||||
final Angel app;
|
||||
Configuration(this.app);
|
||||
|
||||
operator [](key) => app.properties[key];
|
||||
operator []=(key, value) => app.properties[key] = value;
|
||||
|
||||
noSuchMethod(Invocation invocation) {
|
||||
if (invocation.memberName != null) {
|
||||
String name = _sym.firstMatch(invocation.memberName.toString()).group(1);
|
||||
|
||||
if (invocation.isMethod) {
|
||||
return Function.apply(app.properties[name], invocation.positionalArguments,
|
||||
invocation.namedArguments);
|
||||
} else if (invocation.isGetter) {
|
||||
return app.properties[name];
|
||||
}
|
||||
}
|
||||
|
||||
super.noSuchMethod(invocation);
|
||||
}
|
||||
}
|
||||
|
||||
_loadYamlFile(Angel app, File yamlFile) async {
|
||||
if (await yamlFile.exists()) {
|
||||
Map config = loadYaml(await yamlFile.readAsString());
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
name: angel_configuration
|
||||
description: Isomorphic YAML configuration loader for Angel.
|
||||
version: 1.0.1+4
|
||||
version: 1.0.1+6
|
||||
author: Tobe O <thosakwe@gmail.com>
|
||||
homepage: https://github.com/angel-dart/angel_configuration
|
||||
dependencies:
|
||||
analyzer: ">=0.28.1 <0.29.0"
|
||||
angel_framework: ">=1.0.0-dev < 2.0.0"
|
||||
angel_route: ">=1.0.0-dev < 2.0.0"
|
||||
barback: ">=0.15.2 < 0.16.0"
|
||||
yaml: ">= 2.1.8 < 2.2.0"
|
||||
dev_dependencies:
|
||||
|
|
Loading…
Reference in a new issue