| 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'} || {}
MC/DC0%0/1
$app->config->{'snippets'} || {}
$app->config->{'snippets'}
Truth table50%1/2
A
A= $app->config->{'snippets'}
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | my %options = ( | ||||||||||||||||||||
| 57 | %$override, | ||||||||||||||||||||
| 58 | 17 | ▶ | %{ $c->config->{"snippets:$namespace"} || {} }, | ||||||||||||||||||
|
Condition50%1/2
$c->config->{"snippets:$namespace"} || {}
MC/DC0%0/1
$c->config->{"snippets:$namespace"} || {}
$c->config->{"snippets:$namespace"}
Truth table50%1/2
A
A= $c->config->{"snippets:$namespace"}
| |||||||||||||||||||||
| 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
MC/DC0%0/2
$namespace ||= $c->action->name
$namespace
$c->action->name
Truth table33%1/3
A ||= B
A= $namespaceB= $c->action->name
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||