Valiant/Filter.pm

filter: Statementstmt 100% 5 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 6 / 6 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
« Valiant/Filter/Numberize.pm ↑ Summary Valiant/HTML/FormBuilder/DefaultModel.pm »
1 package Valiant::Filter;
2  
3 11 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 11 requires 'filter';
6  
7 11 1;
8  
9 =head1 NAME
10  
11 Valiant::Filter - A role to define the Filter interface.
12  
13 =head1 SYNOPSIS
14  
15 package MySpecialFilter;
16  
17 use Moo;
18 with 'Valiant::Validator';
19  
20 sub filter {
21 my ($self, $class, $attrs) = @_;
22 # Do something with $attrs, possibly with $self
23 return $attrs;
24 }
25  
26 =head1 DESCRIPTION
27  
28 This is a base role for defining a filter. This should be a class that
29 defines a C<filter> method. Here's a more detailed example that shows
30 using a custom filter with a filterable object:
31  
32 package Local::Test::Filter::Truncate;
33  
34 use Moo;
35 with 'Valiant::Filters';
36  
37 has max_length => (is=>'ro', required=>1);
38  
39 sub filter {
40 my ($self, $class, $attrs) = @_;
41 foreach my $key (keys %{$attrs}) {
42 my $current_value = $attrs->{$key};
43 my $new_value = substr( $current_value, 0, $self->max_length );
44 $attrs->{$key} = $new_value;
45 }
46 return $attrs;
47 }
48  
49 package Local::Test::User;
50  
51 use Moo;
52 use Valiant::Filters;
53  
54 has [qw(first_name last_nane)] => (is=>'ro', required=>1);
55  
56 filters_with 'Truncate', max_length=>25;
57  
58 my $user = Local::Test::User->new(
59 first_name => 'averyveryveryveryverylooooooooonnngFIRSTname',
60 last_name => 'averyveryveryveryverylooooooooonnngLASTname',
61 );
62  
63 print $user->first_name; #averyveryveryveryverylooo
64 print $user->last_name; #averyveryveryveryverylooo
65  
66 A Filter receives all the arguments passed to C<new> and runs before the object is created. It gets
67 the following three arguments: C<$self>, which is the instance of the Filter which is associated with
68 the filterable class, C<$class>, which is the name of the class upon which the filter is running and
69 C<$attrs> which is a hashref of the arguments passed to C<new>. C<$attrs> is always a hashref and
70 has been normalized as such. ALso C<$attrs> contains everything passed to C<new> even things that are
71 not valid for the C<$class>.
72  
73 A Filter is created once when the class uses it and exists for the full life cycle
74 of the filterable object.
75  
76 Note that filters run only B<once, at object construction time> - they transform
77 the arguments passed to C<new> and do not re-run on later C<rw> writes or on ORM
78 update paths (see L<Valiant::Filterable/When filters run>).
79  
80 Generally you would write a filter class like this when you want a filter that can be applied
81 to all the incoming arguments or when the filter itself is dependent on all the arguments. For writing
82 filters that are applied to individual attributes you should create a subclass of L<Valiant::Filter::Each>.
83  
84 =head1 PREPACKAGED FILTER CLASSES
85  
86 The following attribute filter classes are shipped with L<Valiant>. Please see the package POD for
87 usage details (this is only a sparse summary)
88  
89 =head2 Collapse
90  
91 Collapse all whitespace in a string to a single space. See L<Valiant::Filter::Collapse> for details.
92  
93 =head2 Flatten
94  
95 Flatten an arrayref or hashref to a string. See L<Valiant::Filter::Flatten> for details.
96  
97 =head2 HtmlEscape
98  
99 Basic HTML escaping on a string. See L<Valiant::Filter::HtmlEscape> for details.
100  
101 =head2 Lower
102  
103 Lowercase a string. See L<Valiant::Filter::Trim> for details.
104  
105 =head2 Title
106  
107 'title' cases a string (all first letters up cased). See L<Valiant::Filter::Title> for details.
108  
109 =head2 ToArray
110  
111 Given a string force to a single element array (do nothing if its already an array.
112 See L<Valiant::Filter::ToArray> for details.
113  
114 =head2 Trim
115  
116 Trims whitespace from the start and/or end of the attribute string.
117  
118 See L<Valiant::Filter::Trim> for details.
119  
120 =head2 UcFirst
121  
122 Uppercases the first letter of a string
123  
124 See L<Valiant::Filter::UcFirst> for details.
125  
126 =head2 Upper
127  
128 Upcase a string. See L<Valiant::Filter::Upper> for details.
129  
130 =head2 With
131  
132 Use a subroutine reference to provide filtering.
133  
134 See L<Valiant::Filter::With> for details.
135  
136 =head2 Template
137  
138 Given a hashref value, return a string based on a template.
139  
140 See L<Valiant::Filter::Template> for details.
141  
142  
143 =head2 Special Validators
144  
145 The following validators are not considered for end users but have documentation you might
146 find useful in furthering your knowledge of L<Valiant>: L<Valiant::Filter::Collection>,
147 L<Valiant::Filter::Each>.
148  
149 =head1 SEE ALSO
150
151 L<Valiant>, L<Valiant::Filter::Each>.
152  
153 =head1 AUTHOR
154
155 See L<Valiant>
156  
157 =head1 COPYRIGHT & LICENSE
158
159 See L<Valiant>
160  
161 =cut
« Valiant/Filter/Numberize.pm ↑ Summary Valiant/HTML/FormBuilder/DefaultModel.pm »