| 1 | package Kelp::Module::Encoder; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 34 | ▶ | use Kelp::Base 'Kelp::Module'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 34 | attr 'args' => undef; | |||||||||||||||||||
| 6 | 45 | ▶ | attr 'encoders' => sub { {} }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | # need to be reimplemented | ||||||||||||||||||||
| 9 | 0 | ▶ | sub encoder_name { ... } | ||||||||||||||||||
|
Subroutine
encoder_name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 10 | 0 | ▶ | sub build_encoder { ... } | ||||||||||||||||||
|
Subroutine
build_encoder: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | sub build | ||||||||||||||||||||
| 13 | 45 | ▶ | { | ||||||||||||||||||
|
Subroutine
build: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 14 | 45 | my ($self, %args) = @_; | |||||||||||||||||||
| 15 | 45 | $self->args(\%args); | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 45 | $self->app->encoder_modules->{$self->encoder_name} = $self; | |||||||||||||||||||
| 18 | } | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | sub get_encoder_config | ||||||||||||||||||||
| 21 | 55 | ▶ | { | ||||||||||||||||||
|
Subroutine
get_encoder_config: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 22 | 55 | my ($self, $name) = @_; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | return { | ||||||||||||||||||||
| 25 | 55 | %{$self->args}, | |||||||||||||||||||
| 26 | 55 | ▶ | %{$self->app->config(join '.', 'encoders', $self->encoder_name, $name) // {}}, | ||||||||||||||||||
|
Condition100%2/2
$self->app->config(join(".", "encoders", $self->encoder_name, $name)) // {}
MC/DC100%1/1
$self->app->config(join(".", "encoders", $self->encoder_name, $name)) // {}
$self->app->config(join(".", "encoders", $self->encoder_name, $name))
Truth table100%2/2
A
A= $self->app->config(join(".", "encoders", $self->encoder_name, $name))
| |||||||||||||||||||||
| 27 | }; | ||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | sub get_encoder | ||||||||||||||||||||
| 31 | 114 | ▶ | { | ||||||||||||||||||
|
Subroutine
get_encoder: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 32 | 114 | my ($self, $name) = @_; | |||||||||||||||||||
| 33 | 114 | ▶ | $name //= 'default'; | ||||||||||||||||||
|
Condition100%2/2
$name //= "default"
MC/DC100%1/1
$name //= "default"
$name
Truth table100%2/2
A
A= $name
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 114 | ▶ | return $self->encoders->{$name} //= | ||||||||||||||||||
|
Condition66%2/3
$self->encoders->{$name} //= $self->build_encoder($self->get_encoder_config($name))
MC/DC0%0/2
$self->encoders->{$name} //= $self->build_encoder($self->get_encoder_config($name))
$self->encoders->{$name}
$self->build_encoder($self->get_encoder_config($name))
Truth table66%2/3
A //= B
A= $self->encoders->{$name}B= $self->build_encoder($self->get_encoder_config($name))
| |||||||||||||||||||||
| 36 | $self->build_encoder($self->get_encoder_config($name)); | ||||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 34 | 1; | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | __END__ | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | =head1 NAME | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | Kelp::Module::Encoder - Base class for encoder modules | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =head1 SYNOPSIS | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | # Writing a new encoder module | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | package My::Encoder; | ||||||||||||||||||||
| 52 | use Kelp::Base 'Kelp::Encoder'; | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | use Some::Class; | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | sub encoder_name { 'something' } | ||||||||||||||||||||
| 57 | sub build_encoder { | ||||||||||||||||||||
| 58 | my ($self, $args) = @_; | ||||||||||||||||||||
| 59 | return Some::Class->new(%$args); | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | sub build { | ||||||||||||||||||||
| 63 | my ($self, %args) = @_; | ||||||||||||||||||||
| 64 | $self->SUPER::build(%args); | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | # rest of module building here if necessary | ||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 1; | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | # configuring a special encoder (in app's configuration) | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | encoders => { | ||||||||||||||||||||
| 74 | something => { | ||||||||||||||||||||
| 75 | modified => { | ||||||||||||||||||||
| 76 | new_argument => 1, | ||||||||||||||||||||
| 77 | }, | ||||||||||||||||||||
| 78 | }, | ||||||||||||||||||||
| 79 | }, | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | # In application's code | ||||||||||||||||||||
| 82 | # will croak if encoder was not loaded | ||||||||||||||||||||
| 83 | # default second argument is 'default' (if not passed) | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | $self->get_encoder('something')->encode; | ||||||||||||||||||||
| 86 | $self->get_encoder(something => 'modified')->decode; | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =head1 DESCRIPTION | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | This is a base class for encoders which want to be compilant with the new | ||||||||||||||||||||
| 91 | L<Kelp/get_encoder> method. L<Kelp::Module::JSON> is one of such modules. | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | This allows to have all encoders in one easy to reach spot rather than a bunch | ||||||||||||||||||||
| 94 | of unrelated methods attached to the main class. It also allows to configure a | ||||||||||||||||||||
| 95 | couple of named encoders with different config in | ||||||||||||||||||||
| 96 | L<Kelp::Module::Config/encoders> configuration of the app. | ||||||||||||||||||||
| 97 | |||||||||||||||||||||