File Coverage

blib/lib/Catalyst/View/JavaScript.pm
Criterion Covered Total %
statement 21 49 42.8
branch 0 20 0.0
condition 0 15 0.0
subroutine 7 10 70.0
pod n/a
total 28 94 29.7


line stmt bran cond sub pod time code
1             package Catalyst::View::JavaScript;
2             our $VERSION = '0.995';
3 1     1   23124 use warnings;
  1         2  
  1         38  
4 1     1   7 use strict;
  1         1  
  1         38  
5 1     1   4857 use MRO::Compat;
  1         4182  
  1         33  
6 1     1   9 use Carp;
  1         2  
  1         113  
7 1     1   1167 use utf8;
  1         12  
  1         7  
8 1     1   1034 use JavaScript::Minifier::XS qw(minify);
  1         2459  
  1         105  
9 1     1   9 use base qw|Catalyst::View|;
  1         3  
  1         1770  
10             __PACKAGE__->mk_accessors(
11             qw(compress disable_if_debug stash key output _cache copyright));
12              
13             sub new {
14 0     0     my $self = shift->next::method(@_);
15 0           my ( $c, $arguments ) = @_;
16 0           my %config = (
17             compress => 1,
18             stash => 1,
19             key => "js",
20             disable_if_debug => 1,
21             %$arguments
22             );
23 0           for my $field ( keys %config ) {
24 0 0         next if $field =~ /^catalyst/;
25 0 0         if ( $self->can($field) ) {
26 0           $self->$field( $config{$field} );
27             }
28             else {
29 0           $c->log->warn("Unknown config parameter '$field'");
30             }
31             }
32 0           return $self;
33             }
34              
35             sub cache {
36 0     0     my $self = shift;
37 0           $self->_cache(@_);
38 0           return $self;
39             }
40              
41             sub process {
42 0     0     my ( $self, $c ) = @_;
43 0           my $data = '';
44 0           my $cached = 0;
45 0 0 0       if ( $self->disable_if_debug && $c->debug ) {
46 0           $self->_cache('');
47 0           $self->compress(0);
48             }
49 0 0 0       if ( $self->_cache
    0 0        
    0 0        
50             && $c->can('cache')
51             && ( $data = $c->cache->get( $self->_cache ) ) )
52             {
53 0           $cached = 1;
54             }
55             elsif ( $self->output ) {
56 0           $data = $c->res->output;
57             }
58             elsif ( $self->stash && $self->key ) {
59 0           $data = $c->stash->{ $self->key };
60             }
61 0 0         unless ($cached) {
62 0 0         $data = minify($data) if ( $self->compress );
63 0 0         $data = "/* " . $self->copyright . " */\n" . $data
64             if ( $self->copyright );
65             }
66              
67 0           $c->res->content_type("text/javascript");
68 0           $c->res->output($data);
69 0 0 0       $c->cache->set( $self->_cache, $data )
70             if ( $self->_cache && $c->can('cache') );
71             }
72              
73             1;
74              
75             __END__
76              
77             =head1 NAME
78              
79             Catalyst::View::JavaScript - Cache and/or compress JavaScript output
80              
81             =head1 VERSION
82              
83             version 0.995
84              
85             =head1 SYNOPSIS
86              
87             This module fetches JavaScript either from the stash or from C<< $c->output >>.
88             By default the JavaScript code is read from C<< $c->stash->{js} >> and compressed.
89             The content type is set to C<text/javascript>.
90              
91             B<By default this view will not compress and/or cache if your application is in the debug mode.>
92              
93             =head1 METHODS
94              
95             =head2 cache
96              
97             If C<$c> is able to cache (i. e. C<< if $c->can('cache') >>) the value of C<cache> is used as key to cache the JavaScript output.
98             This method returns the view so you can invoke it like this:
99              
100             $c->detach( $c->view('JavaScript')->cache('unique-cache-key') );
101              
102             =head2 compress
103              
104             Set this to a true value to enable compression of the code. See L<JavaScript::Minifier::XS> for more information on how this minification works. Defaults to 1.
105              
106             =head2 copyright
107              
108             This string will be displayed on the top of the output enclosed in a commentary tag.
109              
110             =head2 disable_if_debug
111              
112             If you set the debug flag on your application caching and compressing is disabled. Defaults to 1.
113              
114             =head2 output
115              
116             If this is set to a true value this module fetches the JavaScript code from C<< $c->output >> and ignores the value of L</stash>. Defaults to 0.
117              
118             =head2 key
119              
120             This module looks in the stash for this value for JavaScript if L</stash> is enabled. Defaults to C<js>.
121              
122             =head2 stash
123              
124             Set this to a true value if the JavaScript code is on the stash. Set the stash value with L</key>. Defaults to C<1>.
125              
126             =head1 AUTHOR
127              
128             Moritz Onken, C<< <onken at netcubed.de> >>
129              
130             =head1 BUGS
131              
132             Please report any bugs or feature requests to C<bug-catalyst-view-javascript at rt.cpan.org>, or through
133             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-JavaScript>. I will be notified, and then you'll
134             automatically be notified of progress on your bug as I make changes.
135              
136             =head1 COPYRIGHT & LICENSE
137              
138             Copyright 2009 Moritz Onken, all rights reserved.
139              
140             This program is free software; you can redistribute it and/or modify it
141             under the same terms as Perl itself.
142              
143              
144             =cut