From f47e9d48a2d26e92723ae96d3d65a1ba005f426f Mon Sep 17 00:00:00 2001 From: Hakim Cassimally Date: Tue, 15 Jul 2014 17:14:00 +0000 Subject: Open311 Role for accepting default config file See also MooX::ConfigFromFile, but that's underdocumented and seems overengineered -- may be worth implementing if requirements become more complex however. (See also Config::Any, which is well worth doing in future, using YAML only reflects current usage in FMS though.) --- perllib/Open311/Endpoint/Role/ConfigFile.pm | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 perllib/Open311/Endpoint/Role/ConfigFile.pm (limited to 'perllib/Open311/Endpoint/Role/ConfigFile.pm') diff --git a/perllib/Open311/Endpoint/Role/ConfigFile.pm b/perllib/Open311/Endpoint/Role/ConfigFile.pm new file mode 100644 index 000000000..1c4b83355 --- /dev/null +++ b/perllib/Open311/Endpoint/Role/ConfigFile.pm @@ -0,0 +1,30 @@ +package Open311::Endpoint::Role::ConfigFile; +use Moo::Role; +use Path::Tiny 'path'; +use Carp 'croak'; +use YAML (); +use Types::Standard qw( Maybe Str ); + +has config_file => ( + is => 'ro', + isa => Maybe[Str], +); + +around BUILDARGS => sub { + my $next = shift; + my $class = shift; + + my %args = @_; + if (my $config_file = $args{config_file}) { + my $cfg = path($config_file); + croak "$config_file is not a file" unless $cfg->is_file; + + my $config = YAML::LoadFile($cfg) or croak "Couldn't load config from $config_file"; + return $class->$next(%$config, %args); + } + else { + return $class->$next(%args); + } +}; + +1; -- cgit v1.2.3