File Coverage

blib/lib/Kelp/Module/Encoder.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition 6 7 85.7
subroutine 4 6 66.6
pod 1 5 20.0
total 24 33 72.7


line stmt bran cond sub pod time code
1             package Kelp::Module::Encoder;
2              
3 34     34   20870 use Kelp::Base 'Kelp::Module';
  34         102  
  34         232  
4              
5             attr 'args' => undef;
6             attr 'encoders' => sub { {} };
7              
8             # need to be reimplemented
9 0     0 0 0 sub encoder_name { ... }
10 0     0 0 0 sub build_encoder { ... }
11              
12             sub build
13             {
14 45     45 1 164 my ($self, %args) = @_;
15 45         335 $self->args(\%args);
16              
17 45         181 $self->app->encoder_modules->{$self->encoder_name} = $self;
18             }
19              
20             sub get_encoder_config
21             {
22 55     55 0 177 my ($self, $name) = @_;
23              
24             return {
25 55         187 %{$self->args},
26 55   100     153 %{$self->app->config(join '.', 'encoders', $self->encoder_name, $name) // {}},
  55         257  
27             };
28             }
29              
30             sub get_encoder
31             {
32 114     114 0 331 my ($self, $name) = @_;
33 114   100     638 $name //= 'default';
34              
35 114   66     466 return $self->encoders->{$name} //=
36             $self->build_encoder($self->get_encoder_config($name));
37             }
38              
39             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