aboutsummaryrefslogtreecommitdiff
path: root/lib/Config.pm6
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Config.pm6')
-rw-r--r--lib/Config.pm6132
1 files changed, 73 insertions, 59 deletions
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index 16aa7f7..2fa01bf 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -2,7 +2,7 @@
use v6.c;
-use Config::Exception::UnsupportedTypeException;
+use Config::Exception::MissingParserException;
use Config::Exception::UnknownTypeException;
use Config::Exception::FileNotFoundException;
use Config::Type;
@@ -14,36 +14,6 @@ class Config is export
has $!path;
has $!parser;
- multi method read()
- {
- return self.load($!path);
- }
-
- multi method read(Str $path, Str $parser = "")
- {
- Config::Exception::FileNotFoundException.new.throw() unless $path.IO.f;
-
- $!parser = self.get-parser($path, $parser);
-
- require ::($!parser);
- $!content = ::($!parser).read($path);
-
- return True;
- }
-
- multi method read(Hash $hash)
- {
- $!content = $hash;
- }
-
- method write(Str $path, Str $parser = "")
- {
- $parser = self.get-parser($path, $parser);
-
- require ::($parser);
- return ::($parser).write($path, $!content);
- }
-
multi method get(Str $key, Any $default = Nil)
{
my $index = $!content;
@@ -70,33 +40,6 @@ class Config is export
$index;
}
- method has(Str $key) {
- my $index = $!content;
-
- for $key.split(".") -> $part {
- return False unless defined($index{$part});
-
- $index = $index{$part};
- }
-
- True;
- }
-
- method set(Str $key, Any $value)
- {
- my $index := $!content;
-
- for $key.split(".") -> $part {
- $index{$part} = {} unless defined($index{$part});
-
- $index := $index{$part};
- }
-
- $index = $value;
-
- self;
- }
-
method get-parser(Str $path, Str $parser = "")
{
if ($parser ne "") {
@@ -105,7 +48,9 @@ class Config is export
my $type = self.get-parser-type($path);
- Config::Exception::UnknownTypeException.new.throw() if $type eq Config::Type::unknown;
+ Config::Exception::UnknownTypeException.new(
+ type => $type
+ ).throw() if $type eq Config::Type::unknown;
"Config::Parser::" ~ $type;
}
@@ -128,4 +73,73 @@ class Config is export
return Config::Type::unknown;
}
+
+ method has(Str $key) {
+ my $index = $!content;
+
+ for $key.split(".") -> $part {
+ return False unless defined($index{$part});
+
+ $index = $index{$part};
+ }
+
+ True;
+ }
+
+ multi method read()
+ {
+ return self.load($!path);
+ }
+
+ multi method read(Str $path, Str $parser = "")
+ {
+ Config::Exception::FileNotFoundException.new(
+ path => $path
+ ).throw() unless $path.IO.f;
+
+ $!parser = self.get-parser($path, $parser);
+
+ try {
+ CATCH {
+ when X::CompUnit::UnsatisfiedDependency {
+ Config::Exception::MissingParserException.new(
+ parser => $parser
+ ).throw();
+ }
+ }
+
+ require ::($!parser);
+ $!content = ::($!parser).read($path);
+ }
+
+ return True;
+ }
+
+ multi method read(Hash $hash)
+ {
+ $!content = $hash;
+ }
+
+ method set(Str $key, Any $value)
+ {
+ my $index := $!content;
+
+ for $key.split(".") -> $part {
+ $index{$part} = {} unless defined($index{$part});
+
+ $index := $index{$part};
+ }
+
+ $index = $value;
+
+ self;
+ }
+
+ method write(Str $path, Str $parser = "")
+ {
+ $parser = self.get-parser($path, $parser);
+
+ require ::($parser);
+ return ::($parser).write($path, $!content);
+ }
}