aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2017-11-22 13:34:16 +0100
committerPatrick Spek <p.spek@tyil.nl>2017-11-22 13:34:16 +0100
commit11dcaa03fe07fb880fe9f29a7cfaec91043dad81 (patch)
tree6f0cbd13c843c06c90945d9d7535d640f963b9a2
parentf6ee38f9b16030d790fcdb20676f8a78696dc6cb (diff)
Implement .keys method
-rw-r--r--lib/Config.pm625
-rw-r--r--t/07-keys.t22
2 files changed, 47 insertions, 0 deletions
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index d0c1d93..ad9be8a 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -110,6 +110,31 @@ class Config is Associative is export
defined($index);
}
+ method keys()
+ {
+ my @keys;
+
+ for $!content.keys -> $key {
+ @keys.append: self.extract-keys($key);
+ }
+
+ @keys.sort;
+ }
+
+ submethod extract-keys($key)
+ {
+ my $value = self.get($key);
+ return $key if $value !~~ Iterable;
+
+ my @keys;
+
+ for $value.keys -> $nested-key {
+ @keys.append: self.extract-keys("{$key}.{$nested-key}");
+ }
+
+ return @keys;
+ }
+
#| Reload the configuration. Requires the configuration to
#| have been loaded from a file.
multi method read()
diff --git a/t/07-keys.t b/t/07-keys.t
new file mode 100644
index 0000000..225c0b8
--- /dev/null
+++ b/t/07-keys.t
@@ -0,0 +1,22 @@
+#! /usr/bin/env perl6
+
+use v6;
+
+use Config::Parser::NULL;
+use Config;
+use Test;
+
+my Config $c .= new.read: %(
+ "a" => False,
+ "b" => False,
+ "c" => %(
+ "a" => False,
+ "b" => False,
+ ),
+);
+
+my @keys = < a b c.a c.b >;
+
+is $c.keys, @keys, ".keys returns a list of all keys";
+
+# vim: ft=perl6 noet