File Coverage

blib/lib/Brick/Filters.pm
Criterion Covered Total %
statement 9 50 18.0
branch 0 12 0.0
condition 0 15 0.0
subroutine 3 13 23.0
pod n/a
total 12 90 13.3


line stmt bran cond sub pod time code
1             package Brick::Filters;
2              
3 5     5   28 use base qw(Exporter);
  5         8  
  5         462  
4 5     5   24 use vars qw($VERSION);
  5         6  
  5         235  
5              
6             $VERSION = '0.905';
7              
8             package Brick::Bucket;
9 5     5   20 use strict;
  5         9  
  5         3838  
10              
11             =encoding utf8
12              
13             =head1 NAME
14              
15             Brick::Filters - do something to the input data
16              
17             =head1 SYNOPSIS
18              
19             use Brick;
20              
21             =head1 DESCRIPTION
22              
23             =over 4
24              
25             =item _uppercase( HASHREF )
26              
27             This modifies the input data permanently. It removes the non-digits
28             from the specified value in filter_fields. The value is no longer tainted
29             after this runs. It works on all of the fields.
30              
31             filter_fields
32              
33             This filter always succeeds, so it will not generate an validation
34             error.
35              
36             =cut
37              
38             sub _uppercase {
39 0     0     my( $bucket, $setup ) = @_;
40              
41 0           my @caller = $bucket->__caller_chain_as_list();
42              
43             $bucket->add_to_bucket( {
44             name => $setup->{name} || $caller[0]{'sub'},
45             description => "filter: uppercase the input",
46             code => sub {
47 0     0     foreach my $f ( @{ $setup->{filter_fields} } )
  0            
48             {
49 0 0         next unless exists $_[0]->{ $f };
50 0           $_[0]->{ $f } = uc $_[0]->{ $f };
51             }
52 0           return 1;
53             },
54 0   0       } );
55             }
56              
57             =item _lowercase( HASHREF )
58              
59             This modifies the input data permanently. It removes the non-digits
60             from the specified value in filter_fields. The value is no longer tainted
61             after this runs. It works on all of the fields.
62              
63             filter_fields
64              
65             This filter always succeeds, so it will not generate an validation
66             error.
67              
68             =cut
69              
70             sub _lowercase {
71 0     0     my( $bucket, $setup ) = @_;
72              
73 0           my @caller = $bucket->__caller_chain_as_list();
74              
75             $bucket->add_to_bucket( {
76             name => $setup->{name} || $caller[0]{'sub'},
77             description => "filter: uppercase the input",
78             code => sub {
79 0     0     foreach my $f ( @{ $setup->{filter_fields} } )
  0            
80             {
81 0 0         next unless exists $_[0]->{ $f };
82 0           $_[0]->{ $f } = lc $_[0]->{ $f };
83             }
84 0           return 1;
85             },
86 0   0       } );
87             }
88              
89             =item _remove_non_digits( HASHREF )
90              
91             This modifies the input data permanently. It removes the non-digits
92             from the specified value in filter_fields. The value is no longer tainted
93             after this runs. It works on all of the fields.
94              
95             filter_fields
96              
97             This filter always succeeds, so it will not generate an validation
98             error.
99              
100             =cut
101              
102             sub _remove_non_digits {
103 0     0     my( $bucket, $setup ) = @_;
104              
105 0           my @caller = $bucket->__caller_chain_as_list();
106              
107             $bucket->add_to_bucket( {
108             name => $setup->{name} || $caller[0]{'sub'},
109             description => "filter: remove non-digits",
110             code => sub {
111 0     0     foreach my $f ( @{ $setup->{filter_fields} } )
  0            
112             {
113 0 0         next unless exists $_[0]->{ $f };
114 0           $_[0]->{ $f } =~ tr/0-9//cd;
115             $_[0]->{ $f } =
116 0 0         $_[0]->{ $f } =~ m/([0-9]*)/
117             ?
118             $1
119             :
120             '';
121             }
122 0           return 1;
123             },
124 0   0       } );
125             }
126              
127             =item _remove_whitespace( HASHREF )
128              
129             This modifies the input data permanently. It removes the whitespace
130             from the specified value in filter_fields. The value is still tainted
131             after this runs.
132              
133             filter_fields
134              
135             This filter always succeeds, so it will not generate an error.
136              
137             =cut
138              
139             sub _remove_whitespace {
140 0     0     my( $bucket, $setup ) = @_;
141              
142 0           my @caller = $bucket->__caller_chain_as_list();
143              
144             $bucket->add_to_bucket( {
145             name => $setup->{name} || $caller[0]{'sub'},
146             description => "filter: remove whitespace",
147             code => sub {
148 0     0     foreach my $f ( @{ $setup->{filter_fields} } )
  0            
149             {
150 0 0         next unless exists $_[0]->{ $f };
151 0           $_[0]->{ $f } =~ tr/\n\r\t\f //d;
152             }
153             },
154 0   0       } );
155             }
156              
157             =item _remove_extra_fields( HASHREF )
158              
159             This modifies the input data permanently. It removes any fields in
160             the input that are not also in the 'filter_fields' value in HASHREF.
161              
162             filter_fields
163              
164             This filter always succeeds, so it will not generate an error.
165              
166             =cut
167              
168             sub _remove_extra_fields {
169 0     0     my( $bucket, $setup ) = @_;
170              
171 0           my @caller = $bucket->__caller_chain_as_list();
172              
173 0           my %allowed = map { $_, 1 } @{ $setup->{filter_fields} };
  0            
  0            
174              
175             $bucket->add_to_bucket( {
176             name => $setup->{name} || $caller[0]{'sub'},
177             description => "filter: remove extra fields",
178             code => sub {
179 0     0     foreach my $f ( keys % {$_[0] } )
  0            
180             {
181 0 0         delete $_[0]->{$f} unless exists $allowed{$f};
182             }
183             },
184 0   0       } );
185             }
186              
187             =back
188              
189             =head1 TO DO
190              
191             TBA
192              
193             =head1 SEE ALSO
194              
195             TBA
196              
197             =head1 SOURCE AVAILABILITY
198              
199             This source is in Github:
200              
201             https://github.com/briandfoy/brick
202              
203             =head1 AUTHOR
204              
205             brian d foy, C<< >>
206              
207             =head1 COPYRIGHT
208              
209             Copyright © 2007-2026, brian d foy . All rights reserved.
210              
211             You may redistribute this under the terms of the Artistic License 2.0.
212              
213             =cut
214              
215             1;