Imports/Vars.pm

filter: Statementstmt 97.4% 77 / 79 Branchbran 83.3% 10 / 12 Conditioncond 75.0% 12 / 16 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 92.2% 119 / 129 CC 19 SCAR 17.6
CC
19
Cov
93%
export_into
9.0
__ANON__
8.3
export_var_into
3.0
CRAP 5.8
« Imports/Context.pm ↑ Summary Imports.pm »
1 package CatalystX::Imports::Vars;
2  
3 =head1 NAME
4  
5 CatalystX::Imports::Vars - Import application variables
6  
7 =cut
8  
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 BASE CLASSES
13  
14 L<CatalystX::Imports>
15  
16 =cut
17  
18 5 ▶use base 'CatalystX::Imports';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶use Carp::Clan qw{ ^CatalystX::Imports(?:::|$) };
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Data::Alias qw( alias deref );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 SYNOPSIS
24  
25 package MyApp::Controller::Users;
26 use base 'Catalyst::Controller';
27  
28 # use Vars => 1; for just $self, $ctx and @args
29 use CatalystX::Imports
30 Vars => { Stash => [qw( $user $user_rs $template )],
31 Session => [qw( $user_id )] };
32  
33 sub list: Local {
34 $template = 'list.tt';
35 $user_rs = $ctx->model('User')->search;
36 }
37  
38 sub view: Local {
39 $template = 'view.tt';
40 $user = $ctx->model('User')->find($args[0]);
41 }
42  
43 sub me: Local {
44 $ctx->forward('view', [$user_id]);
45 }
46  
47 1;
48  
49 =head1 DESCRIPTION
50  
51 This module allows you to bind various package vars in your controller
52 to specific places in the framework. By default, the variables C<$self>,
53 C<$ctx> and C<@args> are exported. They have the same value as if set
54 via
55  
56 my ($self, $ctx, @args) = @_;
57  
58 in your action.
59  
60 You can use a hash reference to specify what variables you want to bind
61 to their respective fields in the session, flash or stash, as
62 demonstrated in the L</SYNOPSIS>.
63  
64 =cut
65  
66 =head1 METHODS
67  
68 =head2 export_into
69  
70 Exports requested variables and intalls a wrapper to fill them with their
71 respective values if needed.
72  
73 =cut
74  
75 9 ▶sub export_into {
Subroutine export_into: called CC=9 SCAR=22.0
Pod documented
76 9 my ($class, $target, $args) = @_;
77  
78 # a simple '1' means only $self, $ctx and $args are requested
79 9 ▶ if ($args and $args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args and $args == 1) 3 6 9
Condition100%3/3
$args and $args == 1
!ll&&!rl&&r
153
MC/DC100%2/2
$args and $args == 1
$args $args == 1
Truth table100%3/3
A and B
A= $argsB= $args == 1
ABresult
0X0
100
111
80 3 $args = {};
81 }
82  
83 # by now it should be a hash reference, or we got something wrong
84 9 ▶ croak 'Either a 1 or a hash reference expected as argument for Vars'
Branch: 1 branch
BranchTrueFalseTotal
unless $args and ref $args eq "HASH" 2 7 9
Condition100%3/3
$args and ref $args eq "HASH"
!ll&&!rl&&r
117
MC/DC100%2/2
$args and ref $args eq "HASH"
$args ref $args eq "HASH"
Truth table100%3/3
A and B
A= $argsB= ref $args eq "HASH"
ABresult
0X0
100
111
85 unless $args and ref $args eq 'HASH';
86  
87 # fetch session and
88 7 ▶ my @session = @{ $args->{Session} || [] };
Condition100%2/2
$$args{'Session'} || []
l!l
34
MC/DC100%1/1
$$args{'Session'} || []
$$args{'Session'}
Truth table100%2/2
A
A= $$args{'Session'}
Aresult
11
00
89 7 ▶ my @stash = @{ $args->{Stash} || [] };
Condition100%2/2
$$args{'Stash'} || []
l!l
34
MC/DC100%1/1
$$args{'Stash'} || []
$$args{'Stash'}
Truth table100%2/2
A
A= $$args{'Stash'}
Aresult
11
00
90 7 ▶ my @flash = @{ $args->{Flash} || [] };
Condition100%2/2
$$args{'Flash'} || []
l!l
34
MC/DC100%1/1
$$args{'Flash'} || []
$$args{'Flash'}
Truth table100%2/2
A
A= $$args{'Flash'}
Aresult
11
00
91  
92 # build map of symbol hash refs, containing method, type and
93 # sym (name)
94 my @sym =
95 15 map { {method => $_->[0], type => $_->[2], sym => $_->[3]} }
96 15 map { [@$_, $class->_destruct_var_name($_->[1])] }
97 7 map { my $x = $_; map { [$x->[0], $_] } @{ $x->[1] } }
98 [session => \@session], [stash => \@stash], [flash => \@flash];
99  
100 # export all symbols into the requesting namespace, include defaults
101 $class->export_var_into($target, $class->_destruct_var_name($_))
102 7 for @session, @stash, @flash, qw($self $ctx @args);
103  
104 # build and register our action wrapper
105 55 ▶ $class->register_action_wrap_in($target, sub {
Subroutine __ANON__: called CC=8 SCAR=21.2
106 55 my $code = shift;
107 55 my @wrap = @{ shift(@_) };
108 55 my ($self, $ctx, @args) = @_;
109  
110 # install default vars
111 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
112 55 local *{ $target . '::self' } = \$self;
113 55 local *{ $target . '::ctx' } = \$ctx;
114 55 local *{ $target . '::args' } = \@args;
115  
116 # localise symbols to this level
117 55 local *{ "${target}::${_}" } for map { $_->{sym} } @sym;
118  
119 # scalar aliases
120 57 alias ${ "${target}::" . $_->{sym} } =
121 $ctx->can($_->{method})->($ctx)->{ $_->{sym} }
122 55 for grep { $_->{type} eq 'scalar' } @sym;
123  
124 # hash aliases
125 19 alias %{ "${target}::" . $_->{sym} } =
126 19 ▶ %{ $ctx->can($_->{method})->($ctx)->{ $_->{sym} } ||= {} }
Condition0%0/2
$ctx->can($$_{"method"})->($ctx)->{$$_{"sym"}} ||= {}
l!l
00
MC/DC0%0/1
$ctx->can($$_{"method"})->($ctx)->{$$_{"sym"}} ||= {}
$ctx->can($$_{"method"})->($ctx)->{$$_{"sym"}}
Truth table0%0/2
A
A= $ctx->can($$_{"method"})->($ctx)->{$$_{"sym"}}
Aresult
11
00
127 55 for grep { $_->{type} eq 'hash' } @sym;
128  
129 # array aliases
130 19 alias @{ "${target}::" . $_->{sym} } =
131 19 ▶ @{ $ctx->can($_->{method})->($ctx)->{ $_->{sym} } ||= [] }
Condition0%0/2
$ctx->can($$_{"method"})->($ctx)->{$$_{"sym"}} ||= []
l!l
00
MC/DC0%0/1
$ctx->can($$_{"method"})->($ctx)->{$$_{"sym"}} ||= []
$ctx->can($$_{"method"})->($ctx)->{$$_{"sym"}}
Truth table0%0/2
A
A= $ctx->can($$_{"method"})->($ctx)->{$$_{"sym"}}
Aresult
11
00
132 55 for grep { $_->{type} eq 'array' } @sym;
133  
134 # there are other wrappers left
135 55 ▶ if (my $w = shift @wrap) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = shift @wrap) { } 0 55 55
136 0 return $w->($code, \@wrap, @_);
137 }
138  
139 # we're the last wrapper
140 else {
141 55 return $code->(@_);
142 }
143 7 });
144  
145 7 return 1;
146 }
147  
148 =head2 export_var_into
149  
150 Installs a variable into a package.
151  
152 =cut
153  
154 36 ▶sub export_var_into {
Subroutine export_var_into: called CC=3 SCAR=11.0
Pod documented
155 36 my ($class, $target, $type, $name) = @_;
156 36 my $target_name = "${target}::${name}";
157  
158 # initialise exported vars
159 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
160 36 ▶ *$target_name =
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'scalar' ? : 23 13 36
$type eq 'array' ? : 10 3 13
161 ( $type eq 'scalar' ? \$$target_name
162 : $type eq 'array' ? \@$target_name
163 : \%$target_name );
164  
165 36 return 1;
166 }
167  
168 =head2 _destruct_var_name
169  
170 Takes a variable name and returns it's type (C<scalar>, C<array> or
171 C<hash>) and it's symbol parts.
172  
173 =cut
174  
175 51 ▶sub _destruct_var_name {
Subroutine _destruct_var_name: called CC=2 SCAR=7.2
176 51 my ($class, $name) = @_;
177 51 ▶ if ($name =~ /^([\@\%\$])(\S+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^([\@\%\$])(\S+)$/) { } 51 0 51
178 51 my ($sigil, $id) = ($1, $2);
179 51 my %type = qw($ scalar % hash @ array);
180 51 return ($type{ $sigil }, $id);
181 }
182 else {
183 0 croak "Invalid identifier found: '$name'";
184 }
185 }
186  
187 =head1 DIAGNOSTICS
188  
189 =head2 Invalid identifier found: 'foo'
190  
191 You asked for the import of the var 'foo', but it is not a valid variable
192 identifier. E.g.: '@foo', '%foo' and '$foo' are valid, but '-foo', ':foo'
193 and 'foo' are not.
194  
195 =head2 Either a 1 or a hash reference expected as argument for Vars
196  
197 You can import just the default variables ($self, $ctx and @args) by
198 specifying a C<1> as a parameter ...
199  
200 use CatalystX::Imports Vars => 1;
201  
202 ... or you can give it a hash reference and tell it what you want
203 additionally ...
204  
205 use CatalystX::Imports Vars => { Stash => [qw($foo)] };
206  
207 ... but you specified something else as parameter.
208  
209 =head1 SEE ALSO
210  
211 L<Catalyst>,
212 L<CatalystX::Imports>,
213 L<CatalystX::Imports::Context>
214  
215 =head1 AUTHOR AND COPYRIGHT
216  
217 Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>
218  
219 =head1 LICENSE
220  
221 This program is free software; you can redistribute it and/or modify
222 it under the same terms as perl itself.
223  
224 =cut
225  
226  
227 5 1;
« Imports/Context.pm ↑ Summary Imports.pm »