| 1 | package Terse::Plugin::Config; | ||
| 2 | |||
| 3 | 1 | ▶ | use base 'Terse::Plugin'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use Data::LNPath qw/lnpath/; | ||
| 6 | |||
| 7 | sub build_plugin { | ||
| 8 | my ($self) = @_; | ||
| 9 | if (!$self->config_file) { | ||
| 10 | my $file = $0; | ||
| 11 | ($self->config_file = $0) =~ s/(\.psgi)?$/.json/; | ||
| 12 | } | ||
| 13 | $self->{data} = $self->_read_file($self->config_file); | ||
| 14 | return $self; | ||
| 15 | } | ||
| 16 | |||
| 17 | sub find { | ||
| 18 | my ($self, $path) = @_; | ||
| 19 | return lnpath($self->{data}, $path); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub _read_file { | ||
| 23 | my ($self, $file) = @_; | ||
| 24 | open my $fh, '<', $file or die "Cannot open config file: $file"; | ||
| 25 | my $content = do { local $/; <$fh> }; | ||
| 26 | close $fh; | ||
| 27 | $self->_parse_config($content); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub _parse_config { | ||
| 31 | my ($self, $content) = @_; | ||
| 32 | return JSON->new->decode($content); | ||
| 33 | } | ||
| 34 | |||
| 35 | 1; | ||
| 36 | |||
| 37 | __END__ | ||