blib/lib/Catalyst/View/Semantic.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 129 CC 9 SCAR 25.6
CC
9
Cov
0%
layout
20.0
pager
20.0
process
12.0
CRAP 12.9
1 package Catalyst::View::Semantic;
2 # ABSTRACT: Catalyst View for Template::Semantic
3 use Moose;
4 use Template::Semantic;
5 use Data::Page::Pageset();
6 use namespace::autoclean;
7  
8 extends 'Catalyst::View';
9 with 'Catalyst::Component::ApplicationAttribute';
10  
11 has path => (
12 is => 'ro',
13 default => sub { shift->_application->path_to('root','template') }
14 );
15  
16 has engine => (
17 is => 'ro',
18 default => sub { shift->engine_class->new },
19 lazy => 1
20 );
21  
22 has engine_class => (
23 is => 'ro',
24 default => sub { "Template::Semantic" }
25 );
26  
27 has template_extension => (
28 is => 'ro',
29 default => sub { 'html' }
30 );
31  
32 has content_type => (
33 is => 'ro',
34 default => sub { 'text/html' }
35 );
36  
37 has process_key => (
38 is => 'ro',
39 default => sub { 'view' }
40 );
41  
42 has template_key => (
43 is => 'ro',
44 default => sub { 'template' }
45 );
46  
47 has title_key => (
48 is => 'ro',
49 default => sub { 'title' }
50 );
51  
52 has css_key => (
53 is => 'ro',
54 default => sub { 'css' }
55 );
56  
57 has js_key => (
58 is => 'ro',
59 default => sub { 'js' }
60 );
61  
62 has jscode_key => (
63 is => 'ro',
64 default => sub { 'jscode' }
65 );
66  
67 has css_uri => (
68 is => 'ro',
69 default => sub { '/static/css' }
70 );
71  
72 has js_uri => (
73 is => 'ro',
74 default => sub { '/static/js' }
75 );
76  
77 has wrapper => (
78 is => 'ro',
79 default => sub { sub { layout => { body => shift } } }
80 );
81  
82 has pager_template => (
83 is => 'ro',
84 default => sub { 'pager' }
85 );
86  
87 sub process {
88 my ($self, $c, $code, $file) = @_;
89 my $result = $self->render(
90 $file || $c->stash->{$self->template_key} || $c->action,
91 $code || $c->stash->{$self->process_key} || {}
92 );
93 $c->response->body( $self->wrapper ? $self->layout( $self->wrapper->($result), $c->stash )->as_string : $result->as_string );
94 $c->response->content_type($self->content_type) unless $c->response->content_type;
95 }
96  
97 sub render {
98 my ($self, $file, $code) = @_;
99 $self->engine->process(
100 $self->path.'/'.$file.'.'.$self->template_extension,
101 $code
102 );
103 }
104  
105 # layout wrapper
106 sub layout {
107 my ($self, $file, $code, $meta) = @_;
108 $self->render( $file, {
109 '//head' => {
110 'title' => $meta->{$self->title_key},
111 './script[last()-1]' => @{$meta->{js}} ? [map { '@src' => $self->js_uri.'/'.$_.'.js' }, @{$meta->{$self->js_key}}] : undef,
112 './link[last()]' => @{$meta->{css}} ? [map { '@href' => $self->css_uri.'/'.$_.'.css'}, @{$meta->{$self->css_key}}] : undef,
113 $meta->{jscode} ? (
114 './script[last()]' => sub { $_ =~ s/\/\*code\*\//$meta->{$self->jscode_key}/; \$_ }
115 ) : ()
116 },
117 '//html' => $code
118 } );
119 }
120  
121 # pager
122 sub pager {
123 my ($self, $c, $pager, $template) = @_;
124 my $pageset = Data::Page::Pageset->new( $pager );
125 $self->render( ( $template || $self->pager_template ) => {
126 'a.next' => $pager->next_page ? { '@href' => $c->req->uri_with({ page => $pager->next_page }) } : sub {},
127 'a.prev' => $pager->previous_page ? { '@href' => $c->req->uri_with({ page => $pager->previous_page }) } : sub {},
128 'span a' => [ map {
129 '.' => $_,
130 '@href' => $c->req->uri_with({ page => $_ }),
131 $_ eq ($c->req->params->{page} || 1) ? ('@class' => 'current' ) : ()
132 }, ($pageset->current_pageset->first .. $pageset->current_pageset->last) ]
133 } );
134 }
135  
136 __PACKAGE__->meta->make_immutable;
137 1;