Jifty/Request/Mapper.pm

filter: Statementstmt 16.6% 7 / 42 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 50.0% 2 / 4 Podpod 100% 2 / 2 totaltotal 10.8% 11 / 101 CC 21 SCAR 48.9
CC
21
Cov
8%
map
182.0
query_parameters
90.0
BEGIN
1.0
CRAP 132.4
« Jifty/ClassLoader.pm ↑ Summary Jifty/Web.pm »
1 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::Request::Mapper;
5  
6 =head1 NAME
7  
8 Jifty::Request::Mapper - Maps response values into arbitrary query
9 parameters
10  
11 =head1 DESCRIPTION
12  
13 C<Jifty::Request::Mapper> is used to insert values into parameters
14 that you can't know when you originally constructed the request. The
15 prime example of this is a Create action to a View page -- where you
16 can't know what ID to supply to the View page until after the Create
17 action has run. This problem can be fixed by establishing a mapping
18 between some part of the L<Jifty::Result> of the Create action, and
19 the ID query parameter.
20  
21 =head1 METHODS
22  
23 =head2 query_parameters HASH
24  
25 Extended syntax for generating query parameters. This is used by
26 L<Jifty::Web::Form::Clickable> for its C<parameters> argument, as well
27 as for C<results> of continuations.
28  
29 Possible formats for each key => value pair in the C<HASH> are:
30  
31 =over
32  
33 =item C<< KEY => STRING >>
34  
35 The simplest form -- the C<KEY> will have the literal value of the
36 C<STRING> supplied
37  
38 =item C<< KEY => { result => ACTION } >>
39  
40 The C<KEY> will take on the value of the content named C<KEY> from the
41 result of the C<ACTION>. C<ACTION> may either be a L<Jifty::Action>
42 object, or a moniker thereof.
43  
44 =item C<< KEY => { result => ACTION, name => STRING } >>
45  
46 The C<KEY> will take on the value of the content named C<STRING> from
47 the result of the C<ACTION>. C<ACTION> may either be a L<Jifty::Action>
48 object, or a moniker thereof.
49  
50 =item C<< KEY => { request_argument => STRING } >>
51  
52 The C<KEY> will take on the value of the argument named C<STRING> from
53 the request.
54  
55 =item C<< KEY => { argument => ACTION } >>
56  
57 The C<KEY> will take on the value of the argument named C<KEY> from
58 the C<ACTION>. C<ACTION> may either be a L<Jifty::Action> object, or
59 a moniker thereof.
60  
61 =item C<< KEY => { argument => ACTION. name => STRING } >>
62  
63 The C<KEY> will take on the value of the argument named C<STRING> from
64 the C<ACTION>. C<ACTION> may either be a L<Jifty::Action> object, or
65 a moniker thereof.
66  
67 =back
68  
69 C<result_of> and C<argument_to> are valid synonyms for C<result> and
70 C<argument>, above.
71  
72 =cut
73  
74 0 ▶sub query_parameters {
Subroutine query_parameters: not called CC=9 SCAR=45.0
Pod documented
75 0 my $class = shift;
76  
77 0 my %parameters = @_;
78 0 my %return;
79 0 for my $key (keys %parameters) {
80 0 ▶ if (ref $parameters{$key} eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parameters{$key} eq 'HASH') { } 0 0 0
81 0 my %mapping = %{$parameters{$key}};
82  
83 0 ▶ if ($mapping{request_argument}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mapping{"request_argument"}) 0 0 0
84 0 $return{"J:M-$key"} = join("`","A", $mapping{request_argument});
85 }
86  
87 0 for (grep {/^(result(_of)?|argument(_to)?)$/} keys %mapping) {
88 0 my $action = $mapping{$_};
89 0 ▶ my $moniker = ref $action ? $action->moniker : $action;
Branch: 1 branch
BranchTrueFalseTotal
ref $action ? : 0 0 0
90 # If $key is for an argument of an action, we want to
91 # extract only the argument's name, and not just use
92 # the whole encoded J:A:F-... string.
93 0 my (undef, $a, undef) = Jifty::Request->parse_form_field_name($key);
94 0 ▶ my $name = $mapping{name} || $a || $key;
Condition0%0/6
$mapping{'name'} || $a || $key
l!l&&r!l&&!r
000
$mapping{"name"} or $a
l!l&&r!l&&!r
000
MC/DC0%0/3
$mapping{'name'} || $a || $key
$mapping{"name"} $a $key
Truth table0%0/4
A or B || C
A= $mapping{"name"}B= $aC= $key
ABCresult
1XX1
01X1
0011
0000
95  
96 0 ▶ my $type = ($_ =~ /result/) ? "R" : "A";
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /result/ ? : 0 0 0
97  
98 0 $return{"J:M-$key"} = join("`", $type, $moniker, $name);
99 }
100 } else {
101 0 $return{$key} = $parameters{$key};
102 }
103 }
104  
105 0 return %return;
106 }
107  
108  
109 =head2 map PARAMHASH
110  
111 Responsible for doing the actual mapping that L</query_parameters>
112 above sets up. That is, takes magical query parameters and extracts
113 the values they were meant to have.
114  
115 =over
116  
117 =item destination
118  
119 The C<key> from a query parameter
120  
121 =item source
122  
123 The C<value> of a query parameter
124  
125 =item request
126  
127 The L<Jifty::Request> object to pull action arguments from. Defaults
128 to the current request.
129  
130 =item response
131  
132 The L<Jifty::Response> object to pull results from. Defaults to the
133 current response.
134  
135 =back
136  
137 Returns a key => value pair.
138  
139 =cut
140  
141 0 ▶sub map {
Subroutine map: not called CC=13 SCAR=52.0
Pod documented
142 0 my $class = shift;
143  
144 0 my %args = (
145 source => undef,
146 destination => undef,
147 request => Jifty->web->request,
148 response => Jifty->web->response,
149 @_
150 );
151  
152 0 my @original = ($args{destination} => $args{source});
153  
154 # In case the source is a hashref, we force ourselves to go the
155 # *other* direction first.
156 0 ($args{destination}, $args{source}) = $class->query_parameters($args{destination} => $args{source});
157  
158 # Bail unless it's a mapping
159 return ( @original )
160 0 ▶ unless defined $args{destination} and $args{destination} =~ /^J:M-(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"destination"} and $args{"destination"} =~ /^J:M-(.*)/ 0 0 0
Condition0%0/3
defined $args{"destination"} and $args{"destination"} =~ /^J:M-(.*)/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"destination"} and $args{"destination"} =~ /^J:M-(.*)/
defined $args{"destination"} $args{"destination"} =~ /^J:M-(.*)/
Truth table0%0/3
A and B
A= defined $args{"destination"}B= $args{"destination"} =~ /^J:M-(.*)/
ABresult
0X0
100
111
161  
162 0 my $destination = $1;
163  
164 0 my @bits = split( /\`/, $args{source} );
165 0 ▶ if ( $bits[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits[0]) 0 0 0
166 0 ▶ if ( $bits[0] eq "A" and @bits == 3 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($bits[0] eq 'A' and @bits == 3) { } 0 0 0
elsif ($bits[0] eq 'R' and @bits == 3) { } 0 0 0
elsif ($bits[0] eq 'A' and @bits == 2) { } 0 0 0
Condition0%0/3
$bits[0] eq "A" and @bits == 3
!ll&&!rl&&r
000
MC/DC0%0/2
$bits[0] eq "A" and @bits == 3
$bits[0] eq "A" @bits == 3
Truth table0%0/3
A and B
A= $bits[0] eq "A"B= @bits == 3
ABresult
0X0
100
111
167 # No such action -- value is undef
168 0 ▶ return ( $destination => undef ) unless $args{request}->top_request->action( $bits[1] );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"request"}->top_request->action($bits[1]) 0 0 0
169 # We have a value
170 0 return ( $destination => $args{request}->top_request->action( $bits[1] )->argument( $bits[2] ) );
171 ▶ } elsif ( $bits[0] eq "R" and @bits == 3 ) {
Condition0%0/3
$bits[0] eq "R" and @bits == 3
!ll&&!rl&&r
000
MC/DC0%0/2
$bits[0] eq "R" and @bits == 3
$bits[0] eq "R" @bits == 3
Truth table0%0/3
A and B
A= $bits[0] eq "R"B= @bits == 3
ABresult
0X0
100
111
172 # No such action -- value is undef
173 0 ▶ return ( $destination => undef ) unless $args{request}->top_request->action( $bits[1] );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"request"}->top_request->action($bits[1]) 0 0 0
174 # Action exists but hasn't run yet -- defer until later
175 0 ▶ return ( @original ) unless $args{response}->result( $bits[1] );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"response"}->result($bits[1]) 0 0 0
176 # We have a value
177 0 return ( $destination => $args{response}->result( $bits[1] )->content( $bits[2] ) );
178 ▶ } elsif ( $bits[0] eq "A" and @bits == 2 ) {
Condition0%0/3
$bits[0] eq "A" and @bits == 2
!ll&&!rl&&r
000
MC/DC0%0/2
$bits[0] eq "A" and @bits == 2
$bits[0] eq "A" @bits == 2
Truth table0%0/3
A and B
A= $bits[0] eq "A"B= @bits == 2
ABresult
0X0
100
111
179 0 return ( $destination => $args{request}->arguments->{ $bits[1] } );
180 }
181 }
182 # As a fallback, just set it to the value
183 0 return ( $destination => $args{source} );
184  
185 }
186  
187  
188 1 1;
« Jifty/ClassLoader.pm ↑ Summary Jifty/Web.pm »