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   17684 use strict;
  1         1  
  1         29  
22 1     1   3 use vars qw($VERSION $DYNAMIC $AUTOLOAD);
  1         1  
  1         56  
23              
24             $VERSION = '1.04';
25             $DYNAMIC = 1;
26              
27 1     1   454 use Number::Format;
  1         9769  
  1         40  
28 1     1   5 use base qw(Template::Plugin::Filter);
  1         1  
  1         419  
29              
30             # ----------------------------------------------------------------------
31             # filter($text)
32             #
33             # The default filter is format_number, i.e., commify.
34             # ----------------------------------------------------------------------
35             sub filter {
36 1     1 0 87 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 13     13 0 59398 my ($self, $config) = @_;
49 13         14 my ($sub, $filter, $nfo);
50 13         38 $nfo = Number::Format->new(%$config);
51            
52 13         2577 $self->{ _DYNAMIC } = 1;
53 13         16 $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 13         11 for my $sub (@{$Number::Format::EXPORT_TAGS{"subs"}}) {
  13         21  
64             my $filter = sub {
65 12     12   400 my ($context, @args) = @_;
66             return sub {
67 12         254 my $text = shift;
68 12         41 return $nfo->$sub($text, @args);
69 12         42 };
70 91         984 };
71 91         157 $self->{ _CONTEXT }->define_filter($sub, $filter, 1);
72             }
73              
74 13         173 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   51 my $self = shift;
87 1         7 (my $autoload = $AUTOLOAD) =~ s/.*:://;
88              
89 1 50       6 return if $autoload eq 'DESTROY';
90              
91 1         5 $self->{ _NFO }->$autoload(@_);
92             }
93              
94             1;
95              
96             __END__