| 1 | 6 | ▶ | use strict; # make perlcritic happy about the $VERSION that |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 6 | ▶ | use warnings; # Dist::Zilla::Plugin::PkgVersion inserts |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package Template::Provider::PrefixDBIC; | ||
| 5 | 6 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 6 | $Template::Provider::PrefixDBIC::VERSION = '0.01'; | |
| 7 | } | ||
| 8 | |||
| 9 | 6 | ▶ | use parent 'Template::Provider::DBIC'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | sub _init { | ||
| 12 | my ( $self, $options ) = @_; | ||
| 13 | |||
| 14 | $self->{ COLUMN_PREFIX } = $options->{ COLUMN_PREFIX } || 'prefix'; | ||
| 15 | $self->{ PREFIXES } = $options->{ PREFIXES } || ''; | ||
| 16 | |||
| 17 | unless(ref($self->{ PREFIXES })) { | ||
| 18 | $self->{ PREFIXES } = [ $self->{ PREFIXES } ]; | ||
| 19 | } | ||
| 20 | |||
| 21 | if($options->{ SCHEMA }) { | ||
| 22 | return $self->error( | ||
| 23 | __PACKAGE__ . ' does not support the SCHEMA option' | ||
| 24 | ); | ||
| 25 | } | ||
| 26 | unless($options->{ RESULTSET }) { | ||
| 27 | return $self->error("You must provide a DBIx::Class::ResultSet to new"); | ||
| 28 | } | ||
| 29 | |||
| 30 | return $self->SUPER::_init($options); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub prefixes { | ||
| 34 | my $self = shift; | ||
| 35 | |||
| 36 | if(@_) { | ||
| 37 | my $new_prefixes = shift; | ||
| 38 | $new_prefixes = [ $new_prefixes ] unless ref $new_prefixes; | ||
| 39 | $self->{ PREFIXES } = $new_prefixes; | ||
| 40 | } | ||
| 41 | return $self->{ PREFIXES }; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _get_matching_row { | ||
| 45 | my ( $self, $name ) = @_; | ||
| 46 | |||
| 47 | my $prefixes = $self->prefixes; | ||
| 48 | my $rs = $self->{ RESULTSET }; | ||
| 49 | my $name_column = $self->{ COLUMN_NAME }; | ||
| 50 | my $path_column = $self->{ COLUMN_PREFIX }; | ||
| 51 | my $original_path_column = $path_column; | ||
| 52 | |||
| 53 | $name_column = 'me.' . $name_column unless $name_column =~ /\./; | ||
| 54 | $path_column = 'me.' . $path_column unless $path_column =~ /\./; | ||
| 55 | |||
| 56 | my @results = $rs->search({ | ||
| 57 | $name_column => $name, | ||
| 58 | $path_column => {'-in' => $prefixes }, | ||
| 59 | }); | ||
| 60 | |||
| 61 | unless(@results) { | ||
| 62 | return; | ||
| 63 | } | ||
| 64 | |||
| 65 | if(@results > 1) { | ||
| 66 | my $i = 0; | ||
| 67 | my %path_positions = map { $_ => $i++ } @$prefixes; | ||
| 68 | |||
| 69 | @results = sort { | ||
| 70 | $path_positions{$a->get_column($original_path_column)} | ||
| 71 | <=> | ||
| 72 | $path_positions{$b->get_column($original_path_column)} | ||
| 73 | } @results; | ||
| 74 | } | ||
| 75 | |||
| 76 | return $results[0]; # first in the path list | ||
| 77 | } | ||
| 78 | |||
| 79 | sub fetch { | ||
| 80 | my ( $self, $name ) = @_; | ||
| 81 | |||
| 82 | my $compiled_filename = $self->_compiled_filename($name); | ||
| 83 | |||
| 84 | my ( $data, $error ) = $self->_load($name); | ||
| 85 | unless($error) { | ||
| 86 | ( $data, $error ) = $self->_compile($data, $compiled_filename); | ||
| 87 | } | ||
| 88 | $data = $data->{ data } unless $error; | ||
| 89 | |||
| 90 | return ( $data, $error ); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _load { | ||
| 94 | my ( $self, $name ) = @_; | ||
| 95 | |||
| 96 | my $content_column = $self->{ COLUMN_CONTENT }; | ||
| 97 | my $modified_column = $self->{ COLUMN_MODIFIED }; | ||
| 98 | |||
| 99 | my $row = $self->_get_matching_row($name); | ||
| 100 | |||
| 101 | unless($row) { | ||
| 102 | return ( undef, Template::Constants::STATUS_DECLINED ); | ||
| 103 | } | ||
| 104 | |||
| 105 | return ({ | ||
| 106 | name => $name, | ||
| 107 | text => $row->get_column($content_column), | ||
| 108 | time => Date::Parse::str2time($row->get_column($modified_column)), | ||
| 109 | load => time, | ||
| 110 | }, undef); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _template_modified { | ||
| 114 | my ( $self, $name ) = @_; | ||
| 115 | |||
| 116 | my $row = $self->_get_matching_row($name); | ||
| 117 | |||
| 118 | unless($row) { | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | my $mtime = $row->get_column($self->{ COLUMN_MODIFIED }); | ||
| 122 | $mtime = '0E0' if $mtime == 0; | ||
| 123 | return $mtime; | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; | ||
| 127 | |||
| 128 | # ABSTRACT: A template provider that uses DBIC for database access and exhibits fallback functionality | ||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | =pod | ||
| 133 | |||
| 134 | =head1 NAME | ||
| 135 | |||
| 136 | Template::Provider::PrefixDBIC - A template provider that uses DBIC for database access and exhibits fallback functionality | ||
| 137 | |||
| 138 | =head1 VERSION | ||
| 139 | |||
| 140 | version 0.01 | ||
| 141 | |||
| 142 | =head1 SYNOPSIS | ||
| 143 | |||
| 144 | use MySchema; | ||
| 145 | use Template; | ||
| 146 | use Template::Provider::PrefixDBIC; | ||
| 147 | |||
| 148 | my $schema = MySchema->connect(...); | ||
| 149 | my $provider = Template::Provider::PrefixDBIC->new( | ||
| 150 | RESULTSET => $schema->resultset(...), | ||
| 151 | PREFIXES => ['foo', 'default'], | ||
| 152 | ); | ||
| 153 | |||
| 154 | my $template = Template->new({ | ||
| 155 | LOAD_TEMPLATES => [ | ||
| 156 | $provider, | ||
| 157 | ], | ||
| 158 | }); | ||
| 159 | |||
| 160 | $template->process('my_template'); # tries prefix = 'foo', name = 'my_template', then | ||
| 161 | # prefix = 'default', name ='my_template' | ||
| 162 | |||
| 163 | =head1 DESCRIPTION | ||
| 164 | |||
| 165 | Template::Provider::PrefixDBIC combines the fallback functionality of | ||
| 166 | L<Template::Provider> along with the database access of | ||
| 167 | L<Template::Provider::DBIC>. | ||
| 168 | |||
| 169 | If you don't need the fallback functionality, I highly recommend | ||
| 170 | the L<Template::Provider::DBIC> module. | ||
| 171 | |||
| 172 | Because it makes use of the full name of the template, we don't extract | ||
| 173 | a table name from the template name, and the SCHEMA option to the constructor | ||
| 174 | is thus unsupported, unlike in L<Template::Provider::DBIC>. | ||
| 175 | |||
| 176 | =head1 OPTIONS | ||
| 177 | |||
| 178 | In addition to the options provided by L<Template::Provider::DBIC> | ||
| 179 | (with the exception of SCHEMA), Template::Provider::PrefixDBIC also provides | ||
| 180 | the following options: | ||
| 181 | |||
| 182 | =head2 COLUMN_PREFIX | ||
| 183 | |||
| 184 | The table column that contains the prefix for a template entry. This defaults | ||
| 185 | to 'prefix'. | ||
| 186 | |||
| 187 | =head2 PREFIXES | ||
| 188 | |||
| 189 | The list of prefixes that will be used to look up a template. If a string is | ||
| 190 | provided, it is converted to a single-element array reference. Defaults to | ||
| 191 | C<['']>. | ||
| 192 | |||
| 193 | =head1 METHODS | ||
| 194 | |||
| 195 | =head2 $self->prefixes | ||
| 196 | |||
| 197 | =head2 $self->prefixes($new_prefixes) | ||
| 198 | |||
| 199 | When called with no arguments, this method returns the list of prefixes that | ||
| 200 | will be used to look up a template. When called with a C<$new_prefixes> | ||
| 201 | argument, the internal list is replaced with C<$new_prefixes>. | ||
| 202 | C<$new_prefixes> is automatically converted to an array reference if it isn't | ||
| 203 | one. | ||
| 204 | |||
| 205 | =head1 CAUTION | ||
| 206 | |||
| 207 | If your prefix + name combination does not fall under a unique constraint in | ||
| 208 | your database, this module could encounter multiple results for any given | ||
| 209 | prefix/name combination. This module does no extra work to disambiguate; it | ||
| 210 | will simply pick the first one that your database would return from the | ||
| 211 | SELECT, whatever that might be. | ||
| 212 | |||
| 213 | Also, this provider implementation doesn't do any caching. I wrote this module | ||
| 214 | because we need to invoke certain templates based on which customer is accessing | ||
| 215 | a resource, and fallback to a default if there is no customer-specific behavior. | ||
| 216 | I'd rather not let caching leak customer details; maybe I'll add it in a later | ||
| 217 | release. | ||
| 218 | |||
| 219 | =head1 SEE ALSO | ||
| 220 | |||
| 221 | L<Template>, L<Template::Provider>, L<Template::Provider::DBIC> | ||
| 222 | |||
| 223 | =head1 AUTHOR | ||
| 224 | |||
| 225 | Rob Hoelz <rhoelz@inoc.com> | ||
| 226 | |||
| 227 | =head1 COPYRIGHT AND LICENSE | ||
| 228 | |||
| 229 | This software is copyright (c) 2011 by INOC. | ||
| 230 | |||
| 231 | This is free software; you can redistribute it and/or modify it under | ||
| 232 | the same terms as the Perl 5 programming language system itself. | ||
| 233 | |||
| 234 | =cut | ||
| 235 | |||
| 236 | |||
| 237 | __END__ | ||