Brick/Tutorial.pm untested

filter: Statementstmt n/a Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a SCAR -
1 package Brick::Tutorial
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Brick::Tutorial - How to use Brick
8  
9 =head1 SYNOPSIS
10  
11 =head1 DESCRIPTION
12  
13 Brick is a way to organize business rules to validate data. It's easy
14 to validate values by themselves, but the validation of relationships
15 is much trickier.
16  
17 =head2 Making a brick
18  
19 The name "Brick" comes from the terminology for the validation
20 routines, each of which is a brick. Think of the building block toys
21 the come from Denmark and rhyme with "Say Go". Every time I create a
22 brick, I'll add it to the "bucket", which keeps track of all the
23 bricks.
24  
25 Each brick represents one part of a validation, and it should be as
26 targeted as possible. A brick is just a anonymous subroutine that
27 follows an interface.
28  
29 The brick returns it result in one of three ways.
30  
31 =over 4
32  
33 =item Returns true
34  
35 If the brick returns a true value, it means that the data passed its
36 condition.
37  
38 =item Returns false
39  
40 If the brick returns a false value (but didn't die), that means that
41 the data did not pass the condition, but it's not a failure. This is
42 for "selectors", which will let us figure out how to prune validation
43 trees later.
44  
45 =item C<die>s with a reference
46  
47 If a brick C<die>s with a reference, it's like an exception. The brick
48 uses an anonymous hash as the argument to C<die>. That hash contains the
49 name of the brick, a message about why the brick failed, and perhaps a
50 description of the brick.
51  
52 =item C<dies>s with a string
53  
54 If a brick C<die>s with a string, there's a programming problem. This
55 isn't part of the interface, so if this happens, I've messed up
56 somehow.
57  
58 =back
59  
60 Every brick has access to all of the input data. Since I'm concerned
61 about complex relationships, I don't want to limit the effect of any
62 particular piece. With maximum flexibility, I have maximum power.
63  
64 # every brick gets all the input
65 $brick->( \%input );
66  
67 To create a brick, I need an subroutine to do the validation. In this
68 case, I want to ensure that the input hash has a key named 'cat'. If
69 it does I return true, and C<die> with a hash reference otherwise.
70 I'll talk about that C<die> later.
71  
72 my $sub = sub {
73 my $input = shift;
74  
75 return 1 if exists $input->{cat};
76  
77 die {
78 handler => 'Cat key check',
79 failed_field => 'cat'
80 message => "The input didn't have a field named 'cat'",
81 };
82 }
83  
84 The brick doesn't do me much good until I add it to the bucket,
85 though. My call to C<add_to_bucket> returns the anonymous subroutine,
86 but also keeps track of it with a name and a description, as well as
87 the details about which line of code it comes from and many other
88 details.
89  
90 $brick = $bucket->add_to_bucket( {
91 name => 'cat key checker',
92 description => "The input didn't have a field named 'cat'",
93 code => $sub
94 } );
95  
96 The bucket has two major functions: it keeps track of the
97 relationships between bricks so I can "explain" a business rule
98 (discussed later) and so I can easily debug what I've done. Since I'm
99 going to be making a lot of closures, I want to know where they came
100 from in the code. I'd go crazy without being able to use the bucket to
101 help me keep track of things. More on that coming up.
102  
103 I'm going to make a lot of bricks, and some of them will be almost the
104 same. Instead of checking for the 'cat' key, I want to do the same
105 thing but for the key 'dog'. I need a brick factory. The factory gets
106 a C<$setup> hash as an argument. I put the anonymous subroutine inline
107 with the my call to C<add_to_bucket>. Everywhere that I had the
108 literal 'cat' before I now have a variable, C<$setup->{field}>, which
109 came from the input to C<_input_key_exists>.
110  
111 my $cat_brick = $bucket->_input_key_exists( { field => 'cat' } );
112  
113 my $dog_brick = $bucket->_input_key_exists( { field => 'dog' } );
114  
115 Somewhere I defined the C<_input_key_exists> method so it shows
116 up in the Bucket class:
117  
118 package Brick::Bucket;
119  
120 sub _input_key_exists
121 {
122 my( $bucket, $setup ) = @_;
123  
124 $bucket->add_to_bucket( {
125 name => "$setup->{field} key checker",
126 description => "The input didn't have a field named '$setup->{field}'",
127 code => sub {
128 my $input = shift;
129  
130 return 1 if exists $input->{ $setup->{field} };
131  
132 die {
133 handler => 'Cat key check',
134 message => "The input didn't have a field named '$setup->{field}'",
135 };
136 },
137 } );
138  
139 }
140  
141 Every time I call C<_input_key_exists> I get a new brick, because it's
142 always a new closure (closing over C<$setup>). The factory
143 automatically adds the brick to the bucket.
144  
145 =head2 Composing bricks
146  
147 I'll build a business rule from several bricks. When I want to test a
148 business rule, I run all of the bricks and look at their return values.
149 Instead of keeping track of a bunch of bricks, though, I'll compose
150 them into larger structures so I only have to remember one thing. A
151 composer simply creates a new, bigger brick based on the ones I give it. The
152 bucket will keep track of the relationships for me.
153  
154 I create another factory that puts the bricks together. Inside
155 C<_cat_and_dog_exists>, I create the bricks for 'cat' and 'dog', and
156 use them as arguments to C<__compose_satisfy_all>, which creates a new
157 brick that only returns true when both C<$cat_brick> and C<$dog_brick>
158 return true.
159  
160 sub _cat_and_dog_exists
161 {
162 my( $bucket, $setup );
163  
164 my $cat_brick = $bucket->_input_key_exists(
165 { %$setup, field => 'cat' } );
166  
167 my $dog_brick = $bucket->_input_key_exists(
168 { %$setup, field => 'dog' } );
169  
170  
171 $bucket->__compose_satisfy_all( $cat_brick, $dog_brick );
172 }
173  
174 A composer can decide when it should return true, though. In the
175 previous example both bricks had to return true, but if I only need at
176 least one of them to be true, I can use a different composer, perhaps
177 C<__compose_satisfy_any>, in which case only one of the bricks needs
178 to pass:
179  
180 sub _either_cat_and_dog_exists
181 {
182 my( $bucket, $setup );
183  
184 my $cat_brick = $bucket->_input_key_exists(
185 { %$setup, field => 'cat' } );
186  
187 my $dog_brick = $bucket->_input_key_exists(
188 { %$setup, field => 'dog' } );
189  
190  
191 $bucket->__compose_satisfy_any( $cat_brick, $dog_brick );
192 }
193  
194 Brick comes with several composers in C<Brick::Composers>, but you can
195 also create your own if those don't work for you. The composer is
196 really just another factory to create bricks.
197  
198 =head2 Selectors
199  
200 Selectors are a special sort of brick that doesn't C<die>. When it
201 fails, it just returns C<0> (not just false, but specifically 0). I
202 can use these with composers to decide if I want to continue with the
203 rest of the bricks in that composition.
204  
205 The composer C<__compose_pass_or_stop> can use a selector to stop
206 processing. It won't C<die>, so it doesn't fail. It doesn't keep
207 going, either, so it effectively prunes the validation to exclude
208 those bricks that don't apply to the situation.
209  
210 The composer C<__compose_pass_or_skip> usually composes bricks made
211 with C<__compose_pass_or_stop>. Once one thing stops processing, it
212 moves onto the next brick.
213  
214 See the example in C<Brick::Selector>.
215  
216 =head2 Filters
217  
218 Filters are a special sort of brick that always returns true. I use
219 them to affect the input data before I start to validate it.
220  
221 =head1 Making constraints
222  
223 A constraint is a business rule. It's made up of bricks, but it also
224 has some extra glue to connect the input data to the validation
225 routines. The constraints are the end of the line for composition.
226 They are the bits that actually run the bricks and pass the input data
227 to them.
228  
229 Constraints should be public subroutines (so no leading underscores)
230 whose name reflects what it does.
231  
232 sub check_cat_and_dog
233 {
234 my( $bucket, $setup );
235  
236 my $brick = $bucket->_either_cat_and_dog_exists( $setup );
237  
238 my $constraint = $brick->__make_constraint( $brick, $setup );
239 }
240  
241 I don't need to call the constraint subroutines myself. Brick will
242 automatically do that when it constructs a profile.
243  
244 =head1 Making profiles
245  
246 A profile is a collection of constraints to apply to input data. The
247 constraints essentially give an assortment of bricks a name and
248 represent a business rule. The profile represents all of the business
249 rules put together.
250  
251 In data, the profile is a list of anonymous arrays. Each of the
252 anonymous arrays specify three things:
253  
254 =over 4
255  
256 =item A label
257  
258 The label can be anything. It reminds you which profile element you're
259 working with. It doesn't have to be unique, but it should be.
260  
261 =item A constraint name
262  
263 The constraint name refers to the method to use for that business
264 rule. This has to be the name of an existing method (or a code
265 reference returned by can()).
266  
267 =item An anonymous setup hash
268  
269 The last element is a hash of setup information for the bricks. This
270 is the C<$setup> variable seen in the examples.
271  
272 =back
273  
274 Here's a simple profile.
275  
276 my @Profile = (
277 # label #method name #setup
278 [ cat_and_dog => check_cat_and_dog => {} ]
279 );
280  
281 To apply the profile, I pass it along with the input hash to C<apply>:
282  
283 use Brick;
284  
285 my $Brick = Brick->new;
286  
287 my $profile = $Brick->profile_class->new( \@Profile );
288  
289 $Brick->apply( $profile, \%input );
290  
291 Before I apply a profile, I might want to use C<lint> to check it for
292 errors. It's a class method since it hasn't created an object yet:
293  
294 $Brick->profile_class->lint( \@Profile );
295  
296 I can dump the profile in a handy text format with C<explain> to see
297 if it does what I want:
298  
299 $profile->explain;
300  
301  
302 =head1 SOURCE AVAILABILITY
303  
304 This source is in Github:
305  
306 https://github.com/briandfoy/brick
307  
308 =head1 AUTHOR
309  
310 brian d foy, C<< <briandfoy@pobox.com> >>
311  
312 =head1 COPYRIGHT
313  
314 Copyright © 2007-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
315  
316 You may redistribute this under the terms of the Artistic License 2.0.
317  
318 =cut
319  
320 1;