aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2018-08-26 13:51:54 +0200
committerPatrick Spek <p.spek@tyil.nl>2018-08-26 13:51:54 +0200
commit579707dec9cba3e80e6ec5a9908c96c3695e4337 (patch)
tree395f926f7ede3e15dd404cc2c803ae6d6c10fefd
parentdda25363d76b2a408a9c31fcb9e30c643ccbb88c (diff)
Add .clone method
-rw-r--r--CHANGELOG.md4
-rw-r--r--lib/Config.pm610
-rw-r--r--t/09-cloning.t21
3 files changed, 33 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e023e2d..7dd5fe2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic
Versioning](http://semver.org/spec/v2.0.0.html).
+## [UNRELEASED]
+### Added
+- `.clone` method now exists to create a clone of the Config object.
+
## [2.0.0] - 2018-08-26
### Changed
- `.read` will now return the `Config` object, instead of a `Bool`.
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index 78dca0a..0fecadf 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -10,8 +10,8 @@ use Hash::Merge;
unit class Config is Associative;
has Hash $!content = {};
-has Str $!path = "";
-has Str $!parser = "";
+has Str $.path = "";
+has Str $.parser = "";
#| Clear the config.
method clear()
@@ -21,6 +21,12 @@ method clear()
$!parser = "";
}
+method clone (
+ --> Config
+) {
+ Config.new(:$!path, :$!parser).read: $!content;
+}
+
#| Return the entire config hash.
multi method get()
{
diff --git a/t/09-cloning.t b/t/09-cloning.t
new file mode 100644
index 0000000..b13cd7d
--- /dev/null
+++ b/t/09-cloning.t
@@ -0,0 +1,21 @@
+#! /usr/bin/env perl6
+
+use v6.c;
+
+use Test;
+
+use Config;
+
+plan 2;
+
+my Config $a .= new.read: %(
+ foo => "bar",
+ baz => 42,
+);
+
+my Config $b = $a.clone;
+
+is-deeply $a.get, $b.get, "B contains the same data as A";
+isnt $a, $b, "A and B are not the same object";
+
+# vim: ft=perl6 noet