-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: encoder modules through get_encoder
This helps avoid using ugly _json_internal hack and may also be quite useful in other scenarios.
- Loading branch information
Showing
10 changed files
with
224 additions
and
17 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,97 @@ | ||
package Kelp::Module::Encoder; | ||
|
||
use Kelp::Base 'Kelp::Module'; | ||
|
||
attr 'args' => undef; | ||
attr 'encoders' => sub { {} }; | ||
|
||
# need to be reimplemented | ||
sub encoder_name { ... } | ||
sub build_encoder { ... } | ||
|
||
sub build | ||
{ | ||
my ($self, %args) = @_; | ||
$self->args(\%args); | ||
|
||
$self->app->encoder_modules->{$self->encoder_name} = $self; | ||
} | ||
|
||
sub get_encoder_config | ||
{ | ||
my ($self, $name) = @_; | ||
|
||
return { | ||
%{$self->args}, | ||
%{$self->app->config(join '.', 'encoders', $self->encoder_name, $name) // {}}, | ||
}; | ||
} | ||
|
||
sub get_encoder | ||
{ | ||
my ($self, $name) = @_; | ||
$name //= 'default'; | ||
|
||
return $self->encoders->{$name} //= | ||
$self->build_encoder($self->get_encoder_config($name)); | ||
} | ||
|
||
1; | ||
|
||
__END__ | ||
=head1 NAME | ||
Kelp::Module::Encoder - Base class for encoder modules | ||
=head1 SYNOPSIS | ||
# Writing a new encoder module | ||
package My::Encoder; | ||
use Kelp::Base 'Kelp::Encoder'; | ||
use Some::Class; | ||
sub encoder_name { 'something' } | ||
sub build_encoder { | ||
my ($self, $args) = @_; | ||
return Some::Class->new(%$args); | ||
} | ||
sub build { | ||
my ($self, %args) = @_; | ||
$self->SUPER::build(%args); | ||
# rest of module building here if necessary | ||
} | ||
1; | ||
# configuring a special encoder (in app's configuration) | ||
encoders => { | ||
something => { | ||
modified => { | ||
new_argument => 1, | ||
}, | ||
}, | ||
}, | ||
# In application's code | ||
# will croak if encoder was not loaded | ||
# default second argument is 'default' (if not passed) | ||
$self->get_encoder('something')->encode; | ||
$self->get_encoder(something => 'modified')->decode; | ||
=head1 DESCRIPTION | ||
This is a base class for encoders which want to be compilant with the new | ||
L<Kelp/get_encoder> method. L</Kelp::Module::JSON> is one of such modules. | ||
This allows to have all encoders in one easy to reach spot rather than a bunch | ||
of unrelated methods attached to the main class. It also allows to configure a | ||
couple of named encoders with different config in | ||
L<Kelp::Module::Config/encoders> configuration of the app. | ||
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,17 @@ | ||
{ | ||
encoders => { | ||
json => { | ||
indented => { | ||
indent => 1, | ||
}, | ||
}, | ||
}, | ||
|
||
modules_init => { | ||
JSON => { | ||
indent => 0, | ||
space_before => 0, | ||
}, | ||
}, | ||
} | ||
|
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,29 @@ | ||
use Kelp::Base -strict; | ||
|
||
use Kelp; | ||
use Test::More; | ||
use FindBin '$Bin'; | ||
|
||
$ENV{KELP_CONFIG_DIR} = "$Bin/conf/encoders"; | ||
my $app = Kelp->new(mode => 'test'); | ||
|
||
subtest 'testing default encoder' => sub { | ||
my $default_encoder = $app->get_encoder('json'); | ||
my $encoder = $app->get_encoder(json => 'default'); | ||
|
||
ok !$encoder->get_indent, 'encoder no indent ok'; | ||
is $default_encoder, $encoder, 'encoder default key is default ok'; | ||
|
||
ok !$encoder->get_space_before, 'space_before after modification ok'; | ||
$encoder->space_before; | ||
ok $app->get_encoder('json')->get_space_before, 'space_before after modification ok'; | ||
}; | ||
|
||
subtest 'testing default encoder' => sub { | ||
my $encoder = $app->get_encoder(json => 'indented'); | ||
ok $encoder->get_indent, 'encoder extra config ok'; | ||
ok !$encoder->get_space_before, 'encoder no default config ok'; | ||
}; | ||
|
||
done_testing; | ||
|
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