From 7e237a4415ae21c87171c94bad6ddb64788a7c40 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Tue, 10 Sep 2019 11:42:31 -0300
Subject: [PATCH] Bug 23580: Add C4::Context->yaml_preference

This patch adds a new methods to be used on YAML-based sysprefs. The
idea is that we would use it instead of handling the YAML decoding
in-place everywhere we use YAML syprefs.

Ideally, we could even move pipe separated values sysprefs into YAML,
etc.

To test:
- Apply this patches
- Run:
  $ kshell
 k$ prove t/Context.t
=> SUCCESS: Tests pass!
- Sign off :-D

Signed-off-by: Owen Leonard <oleonard@myacpl.org>
---
 C4/Context.pm | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/C4/Context.pm b/C4/Context.pm
index fa612472f75..54341da5dce 100644
--- a/C4/Context.pm
+++ b/C4/Context.pm
@@ -96,6 +96,7 @@ use Encode;
 use File::Spec;
 use Module::Load::Conditional qw(can_load);
 use POSIX ();
+use YAML qw/Load/;
 use ZOOM;
 
 use C4::Boolean;
@@ -437,6 +438,26 @@ sub boolean_preference {
     return defined($it)? C4::Boolean::true_p($it): undef;
 }
 
+=head2 yaml_preference
+
+Retrieves the required system preference value, and converts it
+from YAML into a Perl data structure. It throws an exception if
+the value cannot be properly decoded as YAML.
+
+=cut
+
+sub yaml_preference {
+    my ( $self, $preference ) = @_;
+
+    my $yaml = eval { YAML::Load( $self->preference( $preference ) ); };
+    if ($@) {
+        warn "Unable to parse $preference syspref : $@";
+        return;
+    }
+
+    return $yaml;
+}
+
 =head2 enable_syspref_cache
 
   C4::Context->enable_syspref_cache();
-- 
2.11.0