File Coverage

blib/lib/Template/Plugin/Number/Format.pm
Criterion Covered Total %
statement 32 32 100.0
branch 1 2 50.0
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 41 44 93.1


line stmt bran cond sub pod time code
1             package Template::Plugin::Number::Format;
2              
3             # ----------------------------------------------------------------------
4             # Template::Plugin::Number::Format - Plugin/filter interface to Number::Format
5             # Copyright (C) 2002-2015 Darren Chamberlain
6             #
7             # This program is free software: you can redistribute it and/or modify
8             # it under the terms of the GNU General Public License as published by
9             # the Free Software Foundation, either version 3 of the License, or
10             # (at your option) any later version.
11             #
12             # This program is distributed in the hope that it will be useful,
13             # but WITHOUT ANY WARRANTY; without even the implied warranty of
14             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15             # GNU General Public License for more details.
16             #
17             # You should have received a copy of the GNU General Public License
18             # along with this program. If not, see .
19             # -------------------------------------------------------------------
20              
21 1     1   25393 use strict;
  1         2  
  1         43  
22 1     1   6 use vars qw($VERSION $DYNAMIC $AUTOLOAD);
  1         1  
  1         87  
23              
24             $VERSION = '1.03';
25             $DYNAMIC = 1;
26              
27 1     1   720 use Number::Format;
  1         15996  
  1         51  
28 1     1   7 use base qw(Template::Plugin::Filter);
  1         0  
  1         558  
29              
30             # ----------------------------------------------------------------------
31             # filter($text)
32             #
33             # The default filter is format_number, i.e., commify.
34             # ----------------------------------------------------------------------
35             sub filter {
36 1     1 0 75 my ($self, $text, $args) = @_;
37 1         5 $self->{ _NFO }->format_number($text, @$args);
38             }
39              
40              
41             # ----------------------------------------------------------------------
42             # init($config)
43             #
44             # Initialize the instance. Creates a Number::Format object, which is
45             # used to create closures that implement the filters.
46             # ----------------------------------------------------------------------
47             sub init {
48 14     14 0 64005 my ($self, $config) = @_;
49 14         20 my ($sub, $filter, $nfo);
50 14         45 $nfo = Number::Format->new(%$config);
51            
52 14         2604 $self->{ _DYNAMIC } = 1;
53 14         20 $self->{ _NFO } = $nfo;
54              
55             # ------------------------------------------------------------------
56             # This makes is dependant upon Number::Format not changing the
57             # Exporter interface it advertises, which is unlikely.
58             #
59             # It is likely that each of these subroutines should accept all
60             # the configuration options of the constructor, and instantiate a
61             # new Number::Format instance. This is easier, for now.
62             # ------------------------------------------------------------------
63 14         12 for my $sub (@{$Number::Format::EXPORT_TAGS{"subs"}}) {
  14         24  
64             my $filter = sub {
65 13     13   439 my ($context, @args) = @_;
66             return sub {
67 13         234 my $text = shift;
68 13         37 return $nfo->$sub($text, @args);
69 13         38 };
70 98         997 };
71 98         167 $self->{ _CONTEXT }->define_filter($sub, $filter, 1);
72             }
73              
74 14         191 return $self;
75             }
76              
77             # ----------------------------------------------------------------------
78             # AUTOLOAD
79             #
80             # Catches method calls; so that the plugin can be used like you'd
81             # expect a plugin to work:
82             #
83             # [% USE nf = Number.Format; nf.format_number(num) %]
84             # ----------------------------------------------------------------------
85             sub AUTOLOAD {
86 1     1   31 my $self = shift;
87 1         5 (my $autoload = $AUTOLOAD) =~ s/.*:://;
88              
89 1 50       4 return if $autoload eq 'DESTROY';
90              
91 1         3 $self->{ _NFO }->$autoload(@_);
92             }
93              
94             1;
95              
96             __END__