Re: Petal::Utils patch

Warren Smith <wsmith-o43ciZe/[email protected]>
Newsgroups gmane.comp.lang.perl.modules.petal
Message-ID <[email protected]>
What a crappy patch. Let me try this again...


On Fri, 2005-04-01 at 13:24 -0600, Warren Smith wrote:
> This is against 0.06.
> 
> This adds several new modifiers:
> count: counts items in array
> dollar: formats string as us dollar amount
> percent: formats string as percentage (%.2f%%)
> telephone: formats strings as NANPA phone numbers 
> include: include file with dynamic name
> 
> I added the tests, but it looks like the docmentation may be
> auto-generated, so I didn't touch that.
> 
> Questions, comments, complaints welcome.
> 
> -Warren
patch (text/x-patch, 11.9 KB)
diff -rN ./lib/Petal/Utils/Count.pm ../Petal-Utils-0.07/lib/Petal/Utils/Count.pm
0a1,45
> package Petal::Utils::Count;
> 
> use strict;
> use warnings::register;
> 
> use Carp;
> 
> use base qw( Petal::Utils::Base );
> 
> use constant name    => 'count';
> use constant aliases => qw();
> 
> our $VERSION  = ((require Petal::Utils), $Petal::Utils::VERSION)[1];
> our $REVISION = (split(/ /, ' $Revision: 1.3 $ '))[2];
> 
> sub process {
>   my $class = shift;
>   my $hash  = shift;
>   my $args  = shift || confess( "'count' expects 1 argument (got nothing)!" );
> 
>   my $value = $hash->fetch($args);
>   confess("'count' expects an array!") unless ref $value eq 'ARRAY';
> 
>   return scalar(@$value);
> }
> 
> 
> 
> 1;
> 
> __END__
> 
> Description: The count modifier simply counts the elements in the given 
> array. Very useful for "x row(s) returned" in reports.
> 
> 
> Basic Usage:
>   count: variable
>   	return count of members in variable
> 
> Example:
>   <p petal:content="count: resultset">0</p> row(s) returned.
> 
> See also:
>   Test template t/data/27__count.html for more examples of use.
diff -rN ./lib/Petal/Utils/Dollar.pm ../Petal-Utils-0.07/lib/Petal/Utils/Dollar.pm
0a1,42
> package Petal::Utils::Dollar;
> 
> use strict;
> use warnings::register;
> 
> use Carp;
> 
> use base qw( Petal::Utils::Base );
> 
> use constant name    => 'dollar';
> use constant aliases => qw();
> 
> our $VERSION  = ((require Petal::Utils), $Petal::Utils::VERSION)[1];
> our $REVISION = (split(/ /, ' $Revision: 1.3 $ '))[2];
> 
> sub process {
>   my $class = shift;
>   my $hash  = shift;
>   my $args  = shift || confess( "'dollar' expects 2 arguments (got nothing)!" );
> 
>   my $value = $hash->fetch($args);
>   return sprintf('$%.2f', $value);
> }
> 
> 
> 
> 1;
> 
> __END__
> 
> Description: The dollar modifier returns the argument formatted as US dollars
> 
> 
> Basic Usage:
>   dollar: amount
>   	return amount formatted as US dollars ("$%.2f")
> 
> Example:
>   <p petal:content="dollar: total">$2.00</p>
> 
> See also:
>   Test template t/data/28__dollar.html for more examples of use.
diff -rN ./lib/Petal/Utils/Include.pm ../Petal-Utils-0.07/lib/Petal/Utils/Include.pm
0a1,46
> package Petal::Utils::Include;
> 
> use strict;
> use warnings::register;
> 
> use Carp;
> use Petal;
> use Petal::Hash::String;
> 
> use base qw( Petal::Utils::Base );
> 
> use constant name    => 'include';
> use constant aliases => qw();
> 
> our $VERSION  = ((require Petal::Utils), $Petal::Utils::VERSION)[1];
> our $REVISION = (split(/ /, ' $Revision: 1.3 $ '))[2];
> 
> sub process {
>   my $class = shift;
>   my $hash  = shift;
>   my $args  = shift || confess( "'include' expects 1 argument (got nothing)!" );
> 
>   my $fn = Petal::Hash::String->process($hash, $args);
>   my $template = Petal->new($fn);
>   return $template->process($hash);
> }
> 
> 
> 
> 1;
> 
> __END__
> 
> Description: The include modifier includes a file and processes it. Useful
> for including smaller files into a larger one. It differs from METAL macros
> in that the filename can be a variable.
> 
> Basic Usage:
>   include: filename
>   	Includes and processes given filename for inclusion
> 
> Example:
>   <p petal:content="structure include: ${template}"></p>
> 
> See also:
>   Test template t/data/29__include.html for more examples of use.
diff -rN ./lib/Petal/Utils/Percent.pm ../Petal-Utils-0.07/lib/Petal/Utils/Percent.pm
0a1,46
> package Petal::Utils::Percent;
> 
> use strict;
> use warnings::register;
> 
> use Carp;
> 
> use base qw( Petal::Utils::Base );
> 
> use constant name    => 'percent';
> use constant aliases => qw();
> 
> our $VERSION  = ((require Petal::Utils), $Petal::Utils::VERSION)[1];
> our $REVISION = (split(/ /, ' $Revision: 1.3 $ '))[2];
> 
> sub process {
>   my $class = shift;
>   my $hash  = shift;
>   my $args  = shift || confess( "'count' expects 2 arguments (got nothing)!" );
> 
>   my $value = $hash->fetch($args);
>   $value *= 100 if $value < 1;
>   return sprintf('%.2f%%', $value);
> }
> 
> 
> 
> 1;
> 
> __END__
> 
> Description: The percent modifier formats argument as a percent. Tries to be
> intelligent in that if the given percent is less than 1 (i.e. 0.755), it returns
> 75.50%. If the percent is greater than or equal to one (i.e. 75), percent: 
> returns the percent as a whole value (75.00%).
> 
> Basic Usage:
>   percent: variable
>   	return variable formatted as a percent
> 
> Example:
>   <p petal:content="percent: '0.75'">75.00%</p>
>   <p petal:content="percent: '75.0'">75.00%</p>
> 
> See also:
>   Test template t/data/30__percent.html for more examples of use.
diff -rN ./lib/Petal/Utils/Telephone.pm ../Petal-Utils-0.07/lib/Petal/Utils/Telephone.pm
0a1,44
> package Petal::Utils::Telephone;
> 
> use strict;
> use warnings::register;
> 
> use Carp;
> 
> use base qw( Petal::Utils::Base );
> 
> use constant name    => 'telephone';
> use constant aliases => qw();
> 
> our $VERSION  = ((require Petal::Utils), $Petal::Utils::VERSION)[1];
> our $REVISION = (split(/ /, ' $Revision: 1.3 $ '))[2];
> 
> sub process {
>   my $class = shift;
>   my $hash  = shift;
>   my $args  = shift || confess( "'telephone' expects 2 arguments (got nothing)!" );
> 
>   my $value = $hash->fetch($args);
>   $value =~ s/[^0-9]//g;
>   $value =~ s/(...)(...)(....)/($1) $2-$3/;
>   return $value;
> }
> 
> 
> 
> 1;
> 
> __END__
> 
> Description: The telephone modifier formats argument as a NANPA telephone
> number: (NPA) NXX-XXXX.
> 
> Basic Usage:
>   telephone: variable
>   	return variable formatted as a NANPA telephone number
> 
> Example:
>   <p petal:content="telephone: 8888888888">(888) 888-8888</p>
> 
> See also:
>   Test template t/data/30__telephone.html for more examples of use.
diff -rN ./lib/Petal/Utils.pm ../Petal-Utils-0.07/lib/Petal/Utils.pm
35c35
<    ':all'     => [qw( :default :hash :debug )],
---
>    ':all'     => [qw( :default :hash :debug :file )],
37c37
<    ':text'    => [qw( UpperCase LowerCase UC_First Substr Printf )],
---
>    ':text'    => [qw( UpperCase LowerCase UC_First Substr Printf Dollar Percent Telephone )],
40c40
<    ':list'    => [qw( Sort Limit Limitr)],
---
>    ':list'    => [qw( Sort Limit Limitr Count)],
42a43
>    ':file'    => [qw( Include )],
diff -rN ./MANIFEST ../Petal-Utils-0.07/MANIFEST
61a62,77
> lib/Petal/Utils/Count.pm
> lib/Petal/Utils/Dollar.pm
> lib/Petal/Utils/Include.pm
> lib/Petal/Utils/Percent.pm
> lib/Petal/Utils/Telephone.pm
> t/27__Count.t
> t/28__Dollar.t
> t/29__Include.t
> t/30__Percent.t
> t/31__Telephone.t
> t/data/27__count.html
> t/data/28__dollar.html
> t/data/29__include.html
> t/data/29__included.html
> t/data/30__percent.html
> t/data/31__telephone.html
Binary files ./.MANIFEST.swp and ../Petal-Utils-0.07/.MANIFEST.swp differ
diff -rN ./t/27__Count.t ../Petal-Utils-0.07/t/27__Count.t
0a1,24
> #!/usr/bin/perl
> 
> ##
> ## Tests for Petal::Utils::Count module
> ##
> 
> use blib;
> use strict;
> 
> use Test::More qw(no_plan);
> use Carp;
> use Data::Dumper;
> 
> use t::LoadPetal;
> use Petal::Utils qw( :list );
> 
> my $rows = [ 1 .. 10 ];
> 
> my $template = Petal->new('27__count.html');
> my $out      = $template->process( {
>     rows => $rows,
>   } );
> 
> like($out, qr/count1: 10\n/, 'count1');
diff -rN ./t/28__Dollar.t ../Petal-Utils-0.07/t/28__Dollar.t
0a1,22
> #!/usr/bin/perl
> 
> ##
> ## Tests for Petal::Utils::Dollar module
> ##
> 
> use blib;
> use strict;
> 
> use Test::More qw(no_plan);
> use Carp;
> use Data::Dumper;
> 
> use t::LoadPetal;
> use Petal::Utils qw( :text );
> 
> my $template = Petal->new('28__dollar.html');
> my $out      = $template->process( {
> 	amount => 3.45
>   } );
> 
> like($out, qr/dollar1: \$3.45\n/, 'amount1');
diff -rN ./t/29__Include.t ../Petal-Utils-0.07/t/29__Include.t
0a1,25
> #!/usr/bin/perl
> 
> ##
> ## Tests for Petal::Utils::Include module
> ##
> 
> use blib;
> use strict;
> 
> use Test::More qw(no_plan);
> use Carp;
> use Data::Dumper;
> 
> use t::LoadPetal;
> use Petal::Utils qw( :file );
> 
> my $template = Petal->new('29__include.html');
> 
> my $out      = $template->process( {
> 	filename => '29__included.html'
>   } );
> 
> like($out, qr/include1:\s+Hello, world!/, 'include1');
> 
> 
Binary files ./t/.29__Include.t.swp and ../Petal-Utils-0.07/t/.29__Include.t.swp differ
diff -rN ./t/30__Percent.t ../Petal-Utils-0.07/t/30__Percent.t
0a1,24
> #!/usr/bin/perl
> 
> ##
> ## Tests for Petal::Utils::Percent module
> ##
> 
> use blib;
> use strict;
> 
> use Test::More qw(no_plan);
> use Carp;
> use Data::Dumper;
> 
> use t::LoadPetal;
> use Petal::Utils qw( :text );
> 
> my $template = Petal->new('30__percent.html');
> my $out      = $template->process( {
> 	percent1 => 0.75,
> 	percent2 => 75.5
>   } );
> 
> like($out, qr/percent1: 75\.00\%\n/, 'percent1');
> like($out, qr/percent2: 75\.50\%\n/, 'percent2');
diff -rN ./t/31__Telephone.t ../Petal-Utils-0.07/t/31__Telephone.t
0a1,24
> #!/usr/bin/perl
> 
> ##
> ## Tests for Petal::Utils::Telephone module
> ##
> 
> use blib;
> use strict;
> 
> use Test::More qw(no_plan);
> use Carp;
> use Data::Dumper;
> 
> use t::LoadPetal;
> use Petal::Utils qw( :text );
> 
> my $template = Petal->new('31__telephone.html');
> my $out      = $template->process( {
> 	telephone1 => '333-333-3333',
> 	telephone2 => '4445556666'
>   } );
> 
> like($out, qr/telephone1: \(333\) 333-3333/, 'telephone1');
> like($out, qr/telephone2: \(444\) 555-6666/, 'telephone2');
diff -rN ./t/data/27__count.html ../Petal-Utils-0.07/t/data/27__count.html
0a1,13
> <?xml version="1.0" encoding="utf-8"?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> 
> <html xmlns:tal="http://purl.org/petal/1.0/">
>   <body>
>     <!--? Count -->
>     <div>
> 		count1: <p tal:replace="count: rows">rows</p>
>     </div>
>   </body>
> </html>
> 
diff -rN ./t/data/28__dollar.html ../Petal-Utils-0.07/t/data/28__dollar.html
0a1,13
> <?xml version="1.0" encoding="utf-8"?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> 
> <html xmlns:tal="http://purl.org/petal/1.0/">
>   <body>
>     <!--? Dollar -->
>     <div>
>      	dollar1: <p tal:replace="dollar: amount">amount</p>
>     </div>
>   </body>
> </html>
> 
diff -rN ./t/data/29__included.html ../Petal-Utils-0.07/t/data/29__included.html
0a1,3
> <div petal:omit-tag="">
> 	Hello, world!
> </div>
Binary files ./t/data/.29__included.html.swp and ../Petal-Utils-0.07/t/data/.29__included.html.swp differ
diff -rN ./t/data/29__include.html ../Petal-Utils-0.07/t/data/29__include.html
0a1,13
> <?xml version="1.0" encoding="utf-8"?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> 
> <html xmlns:tal="http://purl.org/petal/1.0/">
>   <body>
>     <!--? Printf -->
>     <div>
>       include1: <p tal:replace="structure include: ${filename}">included file</p>
>     </div>
>   </body>
> </html>
> 
Binary files ./t/data/.29__include.html.swp and ../Petal-Utils-0.07/t/data/.29__include.html.swp differ
diff -rN ./t/data/30__percent.html ../Petal-Utils-0.07/t/data/30__percent.html
0a1,14
> <?xml version="1.0" encoding="utf-8"?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> 
> <html xmlns:tal="http://purl.org/petal/1.0/">
>   <body>
>     <!--? Percent -->
>     <div>
> 		percent1: <p tal:replace="percent: percent1">percent</p>
> 		percent2: <p tal:replace="percent: percent2">percent</p>
>     </div>
>   </body>
> </html>
> 
diff -rN ./t/data/31__telephone.html ../Petal-Utils-0.07/t/data/31__telephone.html
0a1,14
> <?xml version="1.0" encoding="utf-8"?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> 
> <html xmlns:tal="http://purl.org/petal/1.0/">
>   <body>
>     <!--? Telephone -->
>     <div>
> 		telephone1: <p tal:replace="telephone: telephone1">number</p>
> 		telephone2: <p tal:replace="telephone: telephone2">number</p>
>     </div>
>   </body>
> </html>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.