| 1 | package MooseX::Templated::Engine; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | MooseX::Templated::Engine - connects MooseX::Templated object to template | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Internal docs - see L<MooseX::Templated> for usage. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | my $engine = MooseX::Templated::Engine->new( | ||||||||||||||||||||||||||||
| 12 | model => My::Module->new( ... ), | ||||||||||||||||||||||||||||
| 13 | view_class => 'MooseX::Templated::View::TT', | ||||||||||||||||||||||||||||
| 14 | view_config => \%config, | ||||||||||||||||||||||||||||
| 15 | template_suffix => '.tt', | ||||||||||||||||||||||||||||
| 16 | template_root => '__LIB__/root/src', | ||||||||||||||||||||||||||||
| 17 | template_method_stub => '_template', | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | $engine->render(); | ||||||||||||||||||||||||||||
| 21 | # source: | ||||||||||||||||||||||||||||
| 22 | # @INC/root/src/My/Module.tt | ||||||||||||||||||||||||||||
| 23 | # My::Module::_template | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | $engine->render( source => "xml" ); | ||||||||||||||||||||||||||||
| 26 | # source: | ||||||||||||||||||||||||||||
| 27 | # @INC/root/src/My/Module.xml.tt | ||||||||||||||||||||||||||||
| 28 | # My::Module::_template_xml | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | $engine->render( source => "/path/to/file.ext" ); | ||||||||||||||||||||||||||||
| 31 | # source: | ||||||||||||||||||||||||||||
| 32 | # /path/to/file.ext | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | $engine->render( source => \"[% template %]" ); | ||||||||||||||||||||||||||||
| 35 | # source: | ||||||||||||||||||||||||||||
| 36 | # <inline> | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 4 | ▶ | use MooseX::Templated::View::TT; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 4 | ▶ | use MooseX::Templated::Util qw/ where_pm /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 4 | ▶ | use MooseX::Types::Path::Class qw/ Dir /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 4 | ▶ | use Path::Class qw/ file /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 4 | ▶ | use Carp qw/ carp croak /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 4 | our $VERSION = $MooseX::Templated::VERSION; # CPAN complained when VERSION moved to MX::T | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 4 | has 'view' => ( | |||||||||||||||||||||||||||
| 50 | is => 'ro', | ||||||||||||||||||||||||||||
| 51 | does => 'MooseX::Templated::View', | ||||||||||||||||||||||||||||
| 52 | lazy => 1, | ||||||||||||||||||||||||||||
| 53 | builder => '_build_view', | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 3 | ▶ | sub _build_view { | ||||||||||||||||||||||||||
|
Subroutine
_build_view: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 58 | 3 | return $self->view_class->new( model => $self->model ); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 4 | has 'view_class' => ( | |||||||||||||||||||||||||||
| 62 | is => 'ro', | ||||||||||||||||||||||||||||
| 63 | isa => 'ClassName', | ||||||||||||||||||||||||||||
| 64 | default => 'MooseX::Templated::View::TT', | ||||||||||||||||||||||||||||
| 65 | required => 1, | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 4 | has 'template_method_stub' => ( | |||||||||||||||||||||||||||
| 69 | is => 'rw', | ||||||||||||||||||||||||||||
| 70 | isa => 'Str', | ||||||||||||||||||||||||||||
| 71 | lazy => 1, | ||||||||||||||||||||||||||||
| 72 | default => '_template', | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 4 | has 'template_suffix' => ( | |||||||||||||||||||||||||||
| 76 | is => 'rw', | ||||||||||||||||||||||||||||
| 77 | isa => 'Str', | ||||||||||||||||||||||||||||
| 78 | lazy => 1, | ||||||||||||||||||||||||||||
| 79 | builder => '_build_template_suffix', | ||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 3 | ▶ | sub _build_template_suffix { | ||||||||||||||||||||||||||
|
Subroutine
_build_template_suffix: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | 3 | return $self->view->default_template_suffix; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 4 | has 'template_root' => ( | |||||||||||||||||||||||||||
| 88 | is => 'rw', | ||||||||||||||||||||||||||||
| 89 | isa => Dir, | ||||||||||||||||||||||||||||
| 90 | coerce => 1, | ||||||||||||||||||||||||||||
| 91 | default => '__LIB__', | ||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4 | has 'model' => ( | |||||||||||||||||||||||||||
| 95 | is => 'ro', | ||||||||||||||||||||||||||||
| 96 | isa => 'Object', | ||||||||||||||||||||||||||||
| 97 | required => 1, | ||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head2 render() | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 render( source => $source ) | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =cut | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # implemented as sub (rather than handles) so it can be | ||||||||||||||||||||||||||||
| 107 | # 'excluded' in any consuming objects | ||||||||||||||||||||||||||||
| 108 | 14 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | 14 | ▶ | my $args = scalar @_ == 1 ? $_[0] : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # if this is a single string and looks like a shortcut then | ||||||||||||||||||||||||||||
| 113 | # we'll allow this for the moment | ||||||||||||||||||||||||||||
| 114 | 14 | ▶ | if ( !ref $args && $args =~ /^[a-zA-Z0-9_]+$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $args and $args =~ /^[a-zA-Z0-9_]+$/
MC/DC50%1/2
not ref $args and $args =~ /^[a-zA-Z0-9_]+$/
not ref $args
$args =~ /^[a-zA-Z0-9_]+$/
Truth table66%2/3
not(A) and B
A= not ref $argsB= $args =~ /^[a-zA-Z0-9_]+$/
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # https://rt.cpan.org/Public/Bug/Display.html?id=109631 | ||||||||||||||||||||||||||||
| 117 | 2 | local $Carp::Internal{ 'Moose::Meta::Method::Delegation' } = 1; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 2 | carp "DEPRECATED USAGE: render('$args') should be written render(source => '$args')"; | |||||||||||||||||||||||||||
| 120 | 2 | $args = { source => $args }; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 14 | ▶ | if ( ref $args ne 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | croak "ERROR: unexpected arguments to render ($args)"; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 14 | my $source_arg = $args->{source}; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 14 | my $source = $self->get_source( $source_arg ); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 13 | my $model = $self->model; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 13 | return $self->view->process( $source, $model ); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head2 get_source() | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head2 get_source( $shortcut ) | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head2 get_source( \$template ) | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | A shortcut is a simple string such as C<"xml">. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Called from the module C<Farm::Cow>, this will attempt to find the file: | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | @INC/Farm/Cow.xml.tt | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | based on: | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | ${template_root}/Farm/Cow.${shortcut}.${template_suffix} | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Otherwise it will check with the calling object has a method: | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Farm::Cow::_template_xml | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 14 | ▶ | sub get_source { | ||||||||||||||||||||||||||
|
Subroutine
get_source: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 157 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 158 | 14 | ▶ | my $source_type = shift || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 14 | ▶ | if ( ref $source_type eq 'SCALAR' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 1 | return ${ $source_type }; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 13 | my $method_stub = $self->template_method_stub; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 13 | my $source_type_lc = lc( $source_type ); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 13 | ▶ | my $method = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | $source_type | ||||||||||||||||||||||||||||
| 170 | ? $method_stub . "_" . $source_type_lc # _template_xml() | ||||||||||||||||||||||||||||
| 171 | : $method_stub; # _template() | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 13 | ▶ | my $file_suffix = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | $source_type | ||||||||||||||||||||||||||||
| 175 | ? "." . $source_type_lc . $self->template_suffix # .xml.tt | ||||||||||||||||||||||||||||
| 176 | : $self->template_suffix; # .tt | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 13 | my $default_file = $self->build_src_path( suffix => $file_suffix ); | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 13 | ▶ | my $source = $self->model->can( $method ) ? $self->model->$method() | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 181 | : -e $default_file ? file( $default_file )->slurp | ||||||||||||||||||||||||||||
| 182 | : -e $source_type ? file( $source_type )->slurp | ||||||||||||||||||||||||||||
| 183 | : undef; | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 13 | ▶ | if ( ! defined $source ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | croak "[error] ".__PACKAGE__.": couldn't set source:\n". | ||||||||||||||||||||||||||||
| 187 | ( ($source_type =~ /\n/xms) | ||||||||||||||||||||||||||||
| 188 | ? ' - looks like you passed source as "template source" (try \"template source")'."\n" | ||||||||||||||||||||||||||||
| 189 | : ( | ||||||||||||||||||||||||||||
| 190 | " - tried the following tests:\n". | ||||||||||||||||||||||||||||
| 191 | 1 | ▶ | join( "", map { " $_\n" } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | "METHOD: ".blessed( $self->model ) . "::" . $method, | ||||||||||||||||||||||||||||
| 193 | "FILE: ".$default_file, | ||||||||||||||||||||||||||||
| 194 | "FILE: ".$source_type, | ||||||||||||||||||||||||||||
| 195 | ) . | ||||||||||||||||||||||||||||
| 196 | ' - maybe you passed the source as "SCALAR" (try \"SCALAR")'."\n" | ||||||||||||||||||||||||||||
| 197 | ) | ||||||||||||||||||||||||||||
| 198 | ); | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 12 | return $source; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head2 build_src_path( %options ) | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Builds the default filename to be used the template source | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Farm::Cow => /path/to/Farm/Cow.tt | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | template_root class_name template_suffix | ||||||||||||||||||||||||||||
| 211 | /path/to/ Farm/Cow .tt | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head3 options | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | Explicitly passed options override defaults | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =over 8 | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =item 'root' => '/alt/path' | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =item 'suffix' => '.ext' | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =back | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =cut | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 13 | ▶ | sub build_src_path { | ||||||||||||||||||||||||||
|
Subroutine
build_src_path: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 228 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 229 | 13 | ▶ | my $args = @_ == 1 ? $_[0] : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 13 | my $model_class = blessed $self->model; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 13 | my ($abs_file, $inc_path, $require) | |||||||||||||||||||||||||||
| 234 | = MooseX::Templated::Util::where_pm( $model_class ); | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 13 | ▶ | my $root = exists $args->{ root } ? $args->{ root } : $self->template_root; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 13 | ▶ | my $suffix = exists $args->{ suffix } ? $args->{ suffix } : $self->template_suffix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 13 | $root =~ s/__LIB__/$inc_path/mg; | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 13 | my $path = Path::Class::File->new( $root, $require ) . ""; | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 13 | $path =~ s{ .pm $ }{$suffix}xms; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 13 | return $path; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 4 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||
| 250 | __END__ | ||||||||||||||||||||||||||||