File Coverage

blib/lib/Petal/Utils.pm
Criterion Covered Total %
statement 51 56 91.0
branch 14 20 70.0
condition 2 2 100.0
subroutine 11 12 91.6
pod 0 7 0.0
total 78 97 80.4


line stmt bran cond sub pod time code
1             package Petal::Utils;
2              
3             =head1 NAME
4              
5             Petal::Utils - Useful template modifiers for Petal.
6              
7             =head1 SYNOPSIS
8              
9             # install the default set of Petal modifiers:
10             use Petal::Utils;
11              
12             # you can also install modifiers manually:
13             Petal::Utils->install( 'some_modifier', ':some_set' );
14              
15             # see below for modifiers available & template syntax
16              
17             =cut
18              
19 15     15   1469046 use 5.006;
  15         57  
  15         584  
20 15     15   86 use strict;
  15         27  
  15         485  
21 15     15   87 use warnings::register;
  15         30  
  15         2341  
22              
23 15     15   895 use Petal::Hash;
  15         7120  
  15         10682  
24              
25             our $VERSION = '0.06';
26             our $DEBUG = 0;
27              
28             #------------------------------------------------------------------------------
29             # Cusomized import() so the user can select different plugins & sets
30              
31             # use an Exporter-like syntax here:
32             our %PLUGIN_SET =
33             (
34             ':none' => [],
35             ':all' => [qw( :default :hash :debug )],
36             ':default' => [qw( :text :date :logic :list :uri )],
37             ':text' => [qw( UpperCase LowerCase UC_First Substr Printf )],
38             ':logic' => [qw( And If Or Equal Like Decode )],
39             ':date' => [qw( Date US_Date )],
40             ':list' => [qw( Sort Limit Limitr)],
41             ':hash' => [qw( Each Keys )],
42             ':uri' => [qw( UriEscape Create_Href )],
43             ':debug' => [qw( Dump )],
44             );
45              
46             sub import {
47 18     18   4537 my $class = shift;
48 18 50       91 push @_, ':default' unless @_;
49 18         62 return $class->install( @_ );
50             }
51              
52             sub install {
53 40     40 0 80 my $class = shift;
54              
55 40         80 foreach my $item (@_) {
56 99 50       239 next unless $item;
57 99 100       320 if ($item =~ /\A:/) {
58 23         91 $class->install_plugin_set( $item );
59             } else {
60 76         330 $class->install_plugin( $item );
61             }
62             }
63              
64 38         24948 return $class;
65             }
66              
67             sub install_plugin_set {
68 23     23 0 43 my $class = shift;
69 23         36 my $set = shift;
70              
71 23   100     128 my $plugins = $PLUGIN_SET{$set}
72             || die "Can't install non-existent plugin set '$set'!";
73              
74             # recursive so we can have sets of sets:
75 22         208 $class->install( @$plugins );
76             }
77              
78             sub install_plugins {
79 0     0 0 0 my $class = shift;
80 0         0 map { $class->install_plugin( $_ ) } @_;
  0         0  
81 0         0 return $class;
82             }
83              
84             sub install_plugin {
85 76     76 0 104 my $class = shift;
86 76         102 my $name = shift;
87              
88 76         197 my $plugin = $class->find_plugin( $name );
89              
90 75 50       209 warn "installing Petal plugin: '$name'\n" if $DEBUG;
91              
92 75 50       671 if (UNIVERSAL::can($plugin, 'install')) {
93 75         321 $plugin->install;
94             } else {
95 0         0 $Petal::Hash::MODIFIERS->{"$plugin:"} = $plugin;
96             }
97              
98 75         276 return $class;
99             }
100              
101             sub find_plugin {
102 76     76 0 99 my $class = shift;
103 76         96 my $plugin = shift;
104              
105 76 50       704 return \&$plugin if $class->can( $plugin );
106              
107 76 100       198 if (my $plugin_class = $class->load_plugin( $plugin )) {
108 75         223 return $plugin_class;
109             }
110              
111 1         14 die "Can't find Petal plugin: '$plugin'!";
112             }
113              
114             sub load_plugin {
115 76     76 0 103 my $class = shift;
116 76         90 my $plugin = shift;
117              
118 76         180 my $plugin_class = $class->get_plugin_class_for( $plugin );
119 76 100       596 return $plugin_class if $plugin_class->can( 'process' );
120              
121 75         4622 eval "require $plugin_class";
122 75 100       410 if ($@) {
123 1 50       131 warnings::warn("error loading $plugin plugin: $@") if warnings::enabled;
124 1         4 return;
125             }
126              
127 74         292 return $plugin_class;
128             }
129              
130             sub get_plugin_class_for {
131 76     76 0 92 my $class = shift;
132 76         88 my $plugin = shift;
133 76         231 my $plugin_class = "$class\::$plugin";
134             }
135              
136              
137             #------------------------------------------------------------------------------
138             # Plugins
139              
140             ## See Petal::Utils:: for plugin classes
141             ## (plugins are now loaded as needed)
142              
143             ## Alternatively, use subs to insert new modifiers into the Petal Modifiers
144             ## hash. Note that we do not get the $class value in this format.
145              
146             # This style is deprecated:
147             # sub foo {
148             # my $hash = shift;
149             # my $args = shift;
150             # my $result = $hash->fetch( $args );
151             # return 'foo '.$result;
152             # }
153              
154             1;
155              
156             __END__