File Coverage

lib/Web/ComposableRequest/Role/Static.pm
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition 2 2 100.0
subroutine 5 5 100.0
pod 1 1 100.0
total 21 21 100.0


line stmt bran cond sub pod time code
1             package Web::ComposableRequest::Role::Static;
2              
3 1     1   491 use namespace::autoclean;
  1         2  
  1         5  
4              
5 1     1   53 use Web::ComposableRequest::Constants qw( FALSE NUL TRUE );
  1         2  
  1         5  
6 1         5 use Web::ComposableRequest::Util qw( first_char is_arrayref
7 1     1   242 is_hashref new_uri );
  1         2  
8 1     1   321 use Moo::Role;
  1         2  
  1         5  
9              
10             requires qw( locale path query_params scheme _base );
11              
12             around '_build__base' => sub {
13             my ($orig, $self) = @_; $self->mode eq 'static' or return $orig->( $self );
14              
15             my $count = () = split m{ / }mx, $self->path, -1;
16              
17             return '../' x $count;
18             };
19              
20             around '_build_uri' => sub {
21             my ($orig, $self) = @_; $self->mode eq 'static' or return $orig->( $self );
22              
23             my $path = $self->_base.$self->locale.'/'.$self->path.'.html';
24              
25             return new_uri $self->scheme, $path;
26             };
27              
28             around 'uri_for' => sub {
29             my ($orig, $self, $path, @args) = @_; $path //= NUL;
30              
31             ($self->mode eq 'static' and '/' ne substr $path, -1, 1)
32             or return $orig->( $self, $path, @args );
33              
34             my $base = $self->_base; my $extn = '.html';
35              
36             my @query_params = (); my $uri_params = [];
37              
38             if (is_arrayref $args[ 0 ]) {
39             $uri_params = shift @args; @query_params = @args;
40             }
41             elsif (is_hashref $args[ 0 ]) {
42             $uri_params = $args[ 0 ]->{uri_params } // [];
43             @query_params = @{ $args[ 0 ]->{query_params} // [] };
44             $args[ 0 ]->{base } and $base = $args[ 0 ]->{base };
45             $args[ 0 ]->{extension} and $extn = $args[ 0 ]->{extension};
46             }
47              
48             $path or $path = 'index'; $path = $base.$self->locale."/${path}";
49              
50             $uri_params->[ 0 ] and $path = join '/', $path, @{ $uri_params };
51              
52             my $uri = new_uri $self->scheme, $path.$extn;
53              
54             $query_params[ 0 ] and $uri->query_form( @query_params );
55              
56             return $uri;
57             };
58              
59             sub mode {
60 9   100 9 1 36 return $_[ 0 ]->query_params->( 'mode', { optional => TRUE } ) // 'online';
61             }
62              
63             1;
64              
65             __END__