File Coverage

lib/Web/ComposableRequest.pm
Criterion Covered Total %
statement 49 49 100.0
branch 9 10 90.0
condition 6 10 60.0
subroutine 12 12 100.0
pod 1 1 100.0
total 77 82 93.9


line stmt bran cond sub pod time code
1             package Web::ComposableRequest;
2              
3 1     1   446346 use 5.010001;
  1         4  
4 1     1   7 use namespace::autoclean;
  1         3  
  1         32  
5 1     1   95 use version; our $VERSION = qv( sprintf '0.22.%d', q$Rev: 1 $ =~ /\d+/gmx );
  1         2  
  1         10  
6              
7 1     1   154 use Web::ComposableRequest::Constants qw( NUL );
  1         3  
  1         9  
8 1         20 use Unexpected::Types qw( CodeRef HashRef NonEmptySimpleStr
9 1     1   278 Object Undef );
  1         4  
10 1         12 use Web::ComposableRequest::Util qw( first_char is_hashref
11             list_config_roles merge_attributes
12 1     1   4340 trim );
  1         2  
13 1     1   1014 use Scalar::Util qw( blessed );
  1         3  
  1         98  
14 1     1   858 use Web::ComposableRequest::Base;
  1         6  
  1         61  
15 1     1   999 use Web::ComposableRequest::Config;
  1         5  
  1         39  
16 1     1   8 use Moo::Role ();
  1         2  
  1         20  
17 1     1   5 use Moo;
  1         2  
  1         5  
18              
19             has 'buildargs' =>
20             is => 'lazy',
21             isa => CodeRef,
22             default => sub { sub { return $_[1] } };
23              
24             has 'config' =>
25             is => 'lazy',
26             isa => Object,
27             default => sub { $_[0]->config_class->new($_[0]->config_attr) },
28             init_arg => undef;
29              
30             has 'config_attr' =>
31             is => 'ro',
32             isa => HashRef|Object|Undef,
33             init_arg => 'config';
34              
35             has 'config_class' =>
36             is => 'lazy',
37             isa => NonEmptySimpleStr,
38             default => sub {
39             my $base = __PACKAGE__ . '::Config';
40             my @roles = list_config_roles;
41              
42             return $base unless @roles > 0;
43              
44             return Moo::Role->create_class_with_roles($base, @roles);
45             };
46              
47             has 'request_class' =>
48             is => 'lazy',
49             isa => NonEmptySimpleStr,
50             default => sub {
51             my $self = shift;
52             my $base = __PACKAGE__ . '::Base';
53             my $conf = $self->config_attr or return $base;
54             my $dflt = { request_class => $base, request_roles => [] };
55             my $attr = {};
56              
57             merge_attributes $attr, $conf, $dflt, [keys %{$dflt}];
58              
59             my @roles = @{$attr->{request_roles}};
60              
61             return $attr->{request_class} unless @roles > 0;
62              
63             @roles = map { (first_char $_ eq '+')
64             ? substr $_, 1 : __PACKAGE__ . "::Role::${_}" } @roles;
65              
66             return Moo::Role->create_class_with_roles($attr->{request_class}, @roles);
67             };
68              
69             sub new_from_simple_request {
70 7     7 1 2009193 my ($self, $opts, @args) = @_;
71              
72 7   50     17 my $attr = { %{ $opts // {} } };
  7         40  
73 7         204 my $request_class = $self->request_class; # Trigger role application
74              
75 7         10767 $attr->{config} = $self->config; # Composed after request_class
76 7 100 66     176 $attr->{env } = pop @args if @args and is_hashref $args[-1];
77 7 100 66     28 $attr->{params} = pop @args if @args and is_hashref $args[-1];
78              
79 7         18 my $query = $attr->{env}->{'Web::Dispatch::ParamParser.unpacked_query'};
80              
81 7 50       19 $attr->{params} = $query if $query;
82              
83 7 100       16 for my $arg (grep { defined && length } @args) {
  7         37  
84 3 100       8 if (blessed $arg) { $attr->{upload} = $arg }
  1         3  
85             else {
86 2   50     3 push @{ $attr->{args} //= [] }, map { trim $_ } split m{ / }mx, $arg;
  2         15  
  6         11  
87             }
88             }
89              
90 7         120 return $request_class->new($self->buildargs->($self, $attr));
91             }
92              
93             1;
94              
95             __END__