From eb38fa232d7b0727185f9740e5adfae92201f06f Mon Sep 17 00:00:00 2001 From: thosakwe Date: Wed, 23 Nov 2016 15:51:20 -0500 Subject: [PATCH] Inject config --- .travis.yml | 1 + README.md | 8 ++++++++ lib/angel_configuration.dart | 27 ++++++++++++++++++++++++++ pubspec.yaml | 3 ++- test/{all_tests.dart => all_test.dart} | 0 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 .travis.yml rename test/{all_tests.dart => all_test.dart} (100%) diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..de2210c9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1 @@ +language: dart \ No newline at end of file diff --git a/README.md b/README.md index 8128bb88..eb7f38e5 100644 --- a/README.md +++ b/README.md @@ -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; + }); } ``` diff --git a/lib/angel_configuration.dart b/lib/angel_configuration.dart index a685b920..52a66d33 100644 --- a/lib/angel_configuration.dart +++ b/lib/angel_configuration.dart @@ -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()); diff --git a/pubspec.yaml b/pubspec.yaml index f7dcfc94..1ec20992 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 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: diff --git a/test/all_tests.dart b/test/all_test.dart similarity index 100% rename from test/all_tests.dart rename to test/all_test.dart