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
« DBIx/Class/Valiant/locale/errors.pl ↑ Summary Valiant/HTML/FormBuilder/DefaultModel.pm »
1 package Valiant::Filter;
2  
3 15 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 15 requires 'filter';
6  
7 15 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 Generally you would write a filter class like this when you want a filter that can be applied
77 to all the incoming arguments or when the filter itself is dependent on all the arguments. For writing
78 filters that are applied to individual attributes you should create a subclass of L<Valiant::Filter::Each>.
79  
80 =head1 PREPACKAGED FILTER CLASSES
81  
82 The following attribute filter classes are shipped with L<Valiant>. Please see the package POD for
83 usage details (this is only a sparse summary)
84  
85 =head2 Collapse
86  
87 Collapse all whitespace in a string to a single space. See L<Valiant::Filter::Collapse> for details.
88  
89 =head2 Flatten
90  
91 Flatten an arrayref or hashref to a string. See L<Valiant::Filter::Flatten> for details.
92  
93 =head2 HtmlEscape
94  
95 Basic HTML escaping on a string. See L<Valiant::Filter::HtmlEscape> for details.
96  
97 =head2 Lower
98  
99 Lowercase a string. See L<Valiant::Filter::Trim> for details.
100  
101 =head2 Title
102  
103 'title' cases a string (all first letters up cased). See L<Valiant::Filter::Title> for details.
104  
105 =head2 ToArray
106  
107 Given a string force to a single element array (do nothing if its already an array.
108 See L<Valiant::Filter::ToArray> for details.
109  
110 =head2 Trim
111  
112 Trims whitespace from the start and/or end of the attribute string.
113  
114 See L<Valiant::Filter::Trim> for details.
115  
116 =head2 UcFirst
117  
118 Uppercases the first letter of a string
119  
120 See L<Valiant::Filter::UcFirst> for details.
121  
122 =head2 Upper
123  
124 Upcase a string. See L<Valiant::Filter::Upper> for details.
125  
126 =head2 With
127  
128 Use a subroutine reference to provide filtering.
129  
130 See L<Valiant::Filter::With> for details.
131  
132 =head2 Template
133  
134 Given a hashref value, return a string based on a template.
135  
136 See L<Valiant::Filter::Template> for details.
137  
138  
139 =head2 Special Validators
140  
141 The following validators are not considered for end users but have documentation you might
142 find useful in furthering your knowledge of L<Valiant>: L<Valiant::Filter::Collection>,
143 L<Valiant::Filter::Each>.
144  
145 =head1 SEE ALSO
146
147 L<Valiant>, L<Valiant::Filter::Each>.
148  
149 =head1 AUTHOR
150
151 See L<Valiant>
152  
153 =head1 COPYRIGHT & LICENSE
154
155 See L<Valiant>
156  
157 =cut
« DBIx/Class/Valiant/locale/errors.pl ↑ Summary Valiant/HTML/FormBuilder/DefaultModel.pm »