From 5a248da0c49f33d1b578d5b92768f0be35dd6797 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sun, 23 Apr 2017 21:44:40 +0200 Subject: Add multi get() to accept arrays --- lib/Config.pm6 | 15 ++++++++++++++- t/getting.t | 6 +++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/Config.pm6 b/lib/Config.pm6 index 729ccba..abf2b44 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -44,7 +44,7 @@ class Config is export return ::($parser).write($path, $!content); } - method get(Str $key, Any :$default = Nil) + multi method get(Str $key, Any $default = Nil) { my $index = $!content; @@ -57,6 +57,19 @@ class Config is export $index; } + multi method get(@keyparts, Any $default? = Nil) + { + my $index = $!content; + + for @keyparts -> $part { + return $default unless defined($index{$part}); + + $index = $index{$part}; + } + + $index; + } + method has(Str $key) { my $index = $!content; diff --git a/t/getting.t b/t/getting.t index 15937a7..9d0cc5c 100644 --- a/t/getting.t +++ b/t/getting.t @@ -4,7 +4,7 @@ use v6.c; use Test; use lib "lib"; -plan 3; +plan 6; use Config; @@ -20,3 +20,7 @@ $config.read({ ok $config.get("a") eq "a"; ok $config.get("b.c") eq "c"; ok $config.get("nonexistant") === Nil; + +ok $config.get(["a"]) eq "a"; +ok $config.get(["b", "c"]) eq "c"; +ok $config.get(["nonexistant"]) === Nil; -- cgit v1.1