blib/lib/Catalyst/Plugin/Snippets.pm

filter: Statementstmt 96.7% 59 / 61 Branchbran 75.0% 6 / 8 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 94.1% 16 / 17 Podpod 100% 4 / 4 totaltotal 87.1% 88 / 101 CC 8 SCAR 5.5
CC
8
Cov
89%
_snippet_opts
3.1
_snippet_sender
2.1
serve_snippet
2.0
CRAP 1.7
↑ Summary
1 #!/usr/bin/perl
2  
3 package Catalyst::Plugin::Snippets;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶BEGIN { eval { require JSON::Syck } }
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = "0.03";
12  
13 1 ▶sub setup {
Subroutine setup: called CC=2 SCAR=7.0
Pod documented
14 1 my $app = shift;
15 1 my $ret = $app->maybe::next::method(@_);
16  
17 1 %{ $app->config->{snippets} } = (
18 format => "plain",
19 allow_refs => 1,
20 use_session_id => 0,
21 json_content_type => "application/javascript+json",
22 content_type => "text/plain",
23 1 ▶ %{ $app->config->{snippets} || {} },
Condition50%1/2
$app->config->{'snippets'} || {}
l!l
10
MC/DC0%0/1
$app->config->{'snippets'} || {}
$app->config->{'snippets'}
Truth table50%1/2
A
A= $app->config->{'snippets'}
Aresult
11
00
24 );
25  
26 1 $ret;
27 }
28  
29 13 ▶sub snippet {
Subroutine snippet: called CC=2 SCAR=6.9
Pod documented
30 13 my ( $c, $namespace, $key, @args ) = @_;
31  
32 13 ▶ my $meth = @args ? "set" : "get";
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 5 8 13
33  
34 13 my $o = $c->_snippet_opts($namespace);
35  
36 13 my $cache_key = $c->_snippet_key( $namespace, $key, $o );
37  
38 13 $c->cache->$meth( $cache_key, @args );
39 }
40  
41 13 ▶sub _snippet_key {
Subroutine _snippet_key: called CC=2 SCAR=6.9
42 13 my ( $c, $namespace, $key, $options ) = @_;
43  
44 13 my @long_key = ( "snippet", $namespace, $key );
45  
46 13 ▶ push @long_key, $c->sessionid if $options->{use_session_id};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"use_session_id"} 11 2 13
47  
48 13 return join ":", @long_key;
49 }
50  
51 17 ▶sub _snippet_opts {
Subroutine _snippet_opts: called CC=3 SCAR=11.2
52 17 my ( $c, $namespace, @opts ) = @_;
53  
54 17 ▶ my $override = @opts == 1 ? shift @opts : { @opts };
Branch: 1 branch
BranchTrueFalseTotal
@opts == 1 ? : 0 17 17
55  
56 my %options = (
57 %$override,
58 17 ▶ %{ $c->config->{"snippets:$namespace"} || {} },
Condition50%1/2
$c->config->{"snippets:$namespace"} || {}
l!l
017
MC/DC0%0/1
$c->config->{"snippets:$namespace"} || {}
$c->config->{"snippets:$namespace"}
Truth table50%1/2
A
A= $c->config->{"snippets:$namespace"}
Aresult
11
00
59 17 %{ $c->config->{"snippets"} },
60 );
61  
62 17 return \%options;
63 }
64  
65 4 ▶sub serve_snippet {
Subroutine serve_snippet: called CC=2 SCAR=7.1
Pod documented
66 4 my ( $c, $namespace, @_opts ) = @_;
67  
68 4 ▶ $namespace ||= $c->action->name;
Condition33%1/3
$namespace ||= $c->action->name
l!l&&r!l&&!r
040
MC/DC0%0/2
$namespace ||= $c->action->name
$namespace $c->action->name
Truth table33%1/3
A ||= B
A= $namespaceB= $c->action->name
ABresult
1X1
011
000
69  
70 4 my $options = $c->_snippet_opts($namespace, @_opts);
71  
72 4 my $key = join( "/", @{ $c->request->arguments } ); # deparse ;-)
73  
74 4 my $value = $c->snippet( $namespace, $key );
75  
76 4 $c->send_snippet( $value, $options );
77 }
78  
79 4 ▶sub send_snippet {
Subroutine send_snippet: called CC=1 SCAR=0.0
Pod documented
80 4 my ( $c, $value, $options ) = @_;
81  
82 4 $c->_snippet_sender($options)->( $options, $value );
83 }
84  
85 4 ▶sub _snippet_sender {
Subroutine _snippet_sender: called CC=2 SCAR=7.5
86 4 my ( $c, $options ) = @_;
87  
88 4 my $formatter = $options->{format};
89  
90 4 ▶ if ( ref $formatter ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $formatter) { } 0 4 4
91 0 ▶ return sub { $c->_send_snippet( $formatter->( @_ ) ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 } else {
93 4 my $name = "_send_snippet_$formatter";
94 4 ▶ return sub { $c->$name( @_ ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
95 4 }
96 }
97  
98 4 ▶sub _send_snippet {
Subroutine _send_snippet: called CC=1 SCAR=0.0
99 4 my ( $c, $content_type, $body ) = @_;
100 4 $c->response->content_type($content_type);
101 4 $c->response->body($body);
102 }
103  
104 2 ▶sub _send_snippet_json {
Subroutine _send_snippet_json: called CC=1 SCAR=0.0
105 2 my ( $c, $options, $value ) = @_;
106 $c->_send_snippet(
107 $options->{json_content_type},
108 2 JSON::Syck::Dump($value),
109 );
110 }
111  
112 2 ▶sub _send_snippet_plain {
Subroutine _send_snippet_plain: called CC=1 SCAR=0.0
113 2 my ( $c, $options, $value ) = @_;
114 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
115 2 $c->_send_snippet( $options->{content_type}, "$value" );
116 }
117  
118 1 __PACKAGE__;
119  
120 __END__
↑ Summary