-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a configuration system to allow users to set their own defaults
- Loading branch information
Arthur Axel 'fREW' Schmidt
committed
Mar 28, 2015
1 parent
1598864
commit 366d87b
Showing
5 changed files
with
148 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package App::Charon::Config; | ||
|
||
use utf8; | ||
use Moo; | ||
use warnings NONFATAL => 'all'; | ||
|
||
has [qw(listen autoshutdown query_param_auth index)] => ( | ||
is => 'ro', | ||
predicate => 1, | ||
); | ||
|
||
sub opt_default_for { | ||
my ($self, $attr, @default) = @_; | ||
|
||
my $pred = "has_$attr"; | ||
if ($self->$pred) { | ||
return { default => $self->$attr } | ||
} else { | ||
return @default | ||
} | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package App::Charon::ConfigLoader; | ||
|
||
use utf8; | ||
use Moo; | ||
use warnings NONFATAL => 'all'; | ||
|
||
use JSONY; | ||
use IO::All; | ||
use Try::Tiny; | ||
use Module::Runtime 'use_module'; | ||
use namespace::clean; | ||
|
||
has _env_key => ( | ||
is => 'ro', | ||
init_arg => 'env_key', | ||
required => 1, | ||
); | ||
|
||
has _location => ( | ||
is => 'ro', | ||
init_arg => undef, | ||
lazy => 1, | ||
default => sub { | ||
my $self = shift; | ||
$ENV{$self->_env_key . '_CONFLOC'} || $self->__location | ||
}, | ||
); | ||
|
||
has __location => ( | ||
is => 'ro', | ||
init_arg => 'location', | ||
); | ||
|
||
has _config_class => ( | ||
is => 'ro', | ||
init_arg => 'config_class', | ||
required => 1, | ||
); | ||
|
||
sub _io { io->file(shift->_location) } | ||
|
||
sub _read_config_from_file { | ||
my $self = shift; | ||
try { | ||
JSONY->new->load($self->_io->all) | ||
} catch { | ||
{} | ||
} | ||
} | ||
|
||
sub _read_config_from_env { | ||
my $k_re = '^' . quotemeta($_[0]->_env_key) . '_(.+)'; | ||
|
||
+{ | ||
map {; m/$k_re/; lc $1 => $ENV{$_[0]->_env_key . "_$1"} } | ||
grep m/$k_re/, | ||
keys %ENV | ||
} | ||
} | ||
|
||
sub _read_config { | ||
{ | ||
%{$_[0]->_read_config_from_file}, | ||
%{$_[0]->_read_config_from_env}, | ||
} | ||
} | ||
|
||
sub load { use_module($_[0]->_config_class)->new($_[0]->_read_config) } | ||
|
||
1; |