File Coverage

blib/lib/Dancer2/Plugin/Chain/Router.pm
Criterion Covered Total %
statement 44 44 100.0
branch 8 8 100.0
condition n/a
subroutine 11 11 100.0
pod 3 4 75.0
total 66 67 98.5


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::Chain::Router;
2              
3             $Dancer2::Plugin::Chain::Router::VERSION = '0.11';
4             $Dancer2::Plugin::Chain::Router::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Dancer2::Plugin::Chain::Router - Helper package for Dancer2::Plugin::Chain.
9              
10             =head1 VERSION
11              
12             Version 0.11
13              
14             =cut
15              
16 2     2   36 use 5.006;
  2         8  
17 2     2   11 use Data::Dumper;
  2         4  
  2         108  
18              
19 2     2   18 use Moo;
  2         19  
  2         12  
20 2     2   1333 use namespace::autoclean;
  2         22474  
  2         12  
21              
22             has "path_segments" => (is => 'ro', default => sub { [] });
23             has 'code_blocks' => (is => 'ro', default => sub { [] });
24              
25             =head1 DESCRIPTION
26              
27             The helper package for L.
28              
29             =cut
30              
31             sub BUILD {
32 7     7 0 24 my ($self, $args) = @_;
33              
34 7         11 foreach my $segment (@{$args->{args}}) {
  7         14  
35 22 100       49 if (ref($segment) eq __PACKAGE__) {
    100          
36 8         25 $self->_add_path($segment->path_segments);
37 8         17 $self->_add_code($segment->code_blocks);
38             }
39             elsif(ref($segment) eq 'CODE') {
40 8         15 $self->_add_code($segment);
41             }
42             else {
43 6         11 $self->_add_path($segment);
44             }
45             }
46             }
47              
48             =head1 METHODS
49              
50             =head2 path()
51              
52             =cut
53              
54             sub path {
55 3     3 1 5 my $self = shift;
56 3         4 return join '', @{$self->{path_segments}};
  3         13  
57             }
58              
59             =head2 code()
60              
61             =cut
62              
63             sub code {
64 3     3 1 5 my $self = shift;
65              
66 3         5 my @code = @{$self->{code_blocks}};
  3         7  
67             return sub {
68 3     3   181937 my $result;
69 3         10 foreach my $c (@code) {
70 12         328 $result = $c->(@_);
71             }
72 3         83 return $result;
73             }
74 3         32 }
75              
76             =head2 route()
77              
78             =cut
79              
80             sub route {
81 3     3 1 5 my $self = shift;
82              
83 3         7 return ($self->path, $self->code);
84             }
85              
86             #
87             #
88             # PRIVATE METHODS
89              
90             sub _add_path {
91 14     14   20 my ($self, $val) = @_;
92              
93 14 100       90 if (ref($val) eq 'ARRAY') {
94 8         9 push @{$self->{path_segments}}, @$val;
  8         20  
95             }
96             else {
97 6         6 push @{$self->{path_segments}}, $val;
  6         16  
98             }
99             }
100              
101             sub _add_code {
102 16     16   24 my ($self, $val) = @_;
103              
104 16 100       29 if (ref($val) eq 'ARRAY') {
105 8         11 push @{$self->{code_blocks}}, @$val;
  8         20  
106             }
107             else {
108 8         12 push @{$self->{code_blocks}}, $val;
  8         27  
109             }
110             }
111              
112             =head1 AUTHOR
113              
114             Mohammad S Anwar, C<< >>
115              
116             =head1 REPOSITORY
117              
118             L
119              
120             =head1 ACKNOWLEDGEMENTS
121              
122             Inspired by the package L (Yanick Champoux ).
123              
124             =head1 SEE ALSO
125              
126             L
127              
128             =head1 BUGS
129              
130             Please report any bugs or feature requests to C,
131             or through the web interface at L.
132             I will be notified and then you'll automatically be notified of progress on your
133             bug as I make changes.
134              
135             =head1 SUPPORT
136              
137             You can find documentation for this module with the perldoc command.
138              
139             perldoc Dancer2::Plugin::Chain::Router
140              
141             You can also look for information at:
142              
143             =over 4
144              
145             =item * RT: CPAN's request tracker (report bugs here)
146              
147             L
148              
149             =item * AnnoCPAN: Annotated CPAN documentation
150              
151             L
152              
153             =item * CPAN Ratings
154              
155             L
156              
157             =item * Search CPAN
158              
159             L
160              
161             =back
162              
163             =head1 LICENSE AND COPYRIGHT
164              
165             Copyright (C) 2015 - 2016 Mohammad S Anwar.
166              
167             This program is free software; you can redistribute it and / or modify it under
168             the terms of the the Artistic License (2.0). You may obtain a copy of the full
169             license at:
170              
171             L
172              
173             Any use, modification, and distribution of the Standard or Modified Versions is
174             governed by this Artistic License.By using, modifying or distributing the Package,
175             you accept this license. Do not use, modify, or distribute the Package, if you do
176             not accept this license.
177              
178             If your Modified Version has been derived from a Modified Version made by someone
179             other than you,you are nevertheless required to ensure that your Modified Version
180             complies with the requirements of this license.
181              
182             This license does not grant you the right to use any trademark, service mark,
183             tradename, or logo of the Copyright Holder.
184              
185             This license includes the non-exclusive, worldwide, free-of-charge patent license
186             to make, have made, use, offer to sell, sell, import and otherwise transfer the
187             Package with respect to any patent claims licensable by the Copyright Holder that
188             are necessarily infringed by the Package. If you institute patent litigation
189             (including a cross-claim or counterclaim) against any party alleging that the
190             Package constitutes direct or contributory patent infringement,then this Artistic
191             License to you shall terminate on the date that such litigation is filed.
192              
193             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
194             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
195             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
196             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
197             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
198             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
199             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
200              
201             =cut
202              
203             1; # End of Dancer2::Plugin::Chain::Router