Factory.pm

filter: Statementstmt 100% 98 / 98 Branchbran 81.8% 18 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 29 / 29 Podpod 100% 18 / 18 totaltotal 97.6% 163 / 167 CC 16 SCAR 7.6
CC
16
Cov
97%
base_factory
5.0
create
4.0
exclude
3.0
CRAP 2.1
« Factory/Fields.pm ↑ Summary
1 package DBIx::Class::Factory;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use DBIx::Class::Factory::Fields;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =encoding utf8
9  
10 =head1 NAME
11  
12 DBIx::Class::Factory - factory-style fixtures for DBIx::Class
13  
14 =head1 VERSION
15  
16 Version 0.03
17  
18 =cut
19  
20 1 our $VERSION = '0.03';
21  
22 =head1 SYNOPSIS
23  
24 Create factory:
25  
26 package My::UserFactory;
27 use base qw(DBIx::Class::Factory);
28  
29 __PACKAGE__->resultset(My::Schema->resultset('User'));
30 __PACKAGE__->fields({
31 name => __PACKAGE__->seq(sub {'User #' . shift}),
32 status => 'new',
33 });
34  
35 package My::SuperUserFactory;
36 use base qw(DBIx::Class::Factory);
37  
38 __PACKAGE__->base_factory('My::UserFactory');
39 __PACKAGE__->field(superuser => 1);
40  
41 Use factory:
42  
43 my $user = My::UserFactory->create();
44 my @verified_users = @{ My::UserFactory->create_batch(3, {status => 'verified'}) };
45  
46 my $superuser = My::SuperUserFactory->build();
47 $superuser->insert();
48  
49 =head1 DESCRIPTION
50  
51 Ruby has C<factory_girl>, Python has C<factory_boy>. Now Perl has C<DBIx::Class::Factory>.
52  
53 Creating big fixture batches may be a pain. This module provides easy way
54 of creating data in database via L<DBIx::Class>.
55  
56 To create a factory just derive from L<DBIx::Class::Factory> and apply some settings.
57 You can also add some data at the moment of creating instance, redefining factory defaults.
58  
59 Tests for this module contains a bunch of usefull examples.
60  
61 =head1 METHODS
62  
63 =head2 Factory settings
64  
65 =over
66  
67 =item B<base_factory>
68  
69 Use this to create one factory derived from another. Don't use direct inheritance.
70  
71 =cut
72  
73 sub base_factory {
74 3 ▶ my ($class, $base_class) = @_;
Subroutine base_factory: called CC=5 SCAR=16.1
Pod documented
75  
76 3 foreach my $data_field (qw(fields exclude)) {
77 $class->_class_data->{$data_field} = {
78 6 ▶ %{ $base_class->_class_data->{$data_field} || {} },
Branch: 1 branch
BranchTrueFalseTotal
$base_class->_class_data->{$data_field} or {} 2 4 6
79 6 ▶ %{ $class ->_class_data->{$data_field} || {} },
Branch: 1 branch
BranchTrueFalseTotal
$class->_class_data->{$data_field} or {} 3 3 6
80 };
81 }
82  
83 $class->_class_data->{resultset} = $base_class->_class_data->{resultset}
84 3 ▶ unless defined $class->_class_data->{resultset};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class->_class_data->{"resultset"} 3 0 3
85  
86 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 3 push(@{$class . '::ISA'}, $base_class);
88  
89 3 return;
90 }
91  
92 =item B<resultset>
93  
94 Set resultset this factory is going to work with.
95  
96 =cut
97  
98 sub resultset {
99 4 ▶ my ($class, $resultset) = @_;
Subroutine resultset: called CC=1 SCAR=0.0
Pod documented
100  
101 4 $class->_class_data->{resultset} = $resultset;
102  
103 4 return;
104 }
105  
106 =item B<fields>
107  
108 Accept hashref as an argument. Add fields to factory. See L</field> for more details.
109  
110 =cut
111  
112 sub fields {
113 6 ▶ my ($class, $fields) = @_;
Subroutine fields: called CC=2 SCAR=6.9
Pod documented
114  
115 6 foreach my $key (keys %{$fields}) {
116 11 $class->field($key => $fields->{$key});
117 }
118  
119 6 return;
120 }
121  
122 =item B<field>
123  
124 __PACKAGE__->field($name => $value);
125  
126 Add field to the factory. C<$name> is directly used in resultset's C<new> method.
127 C<$value> must be any value or helper result (see L</Helpers>).
128 C<CODEREF> as a value will be used as callback. However, you must not rely on this,
129 it can be changed in future releases — use L</callback> helper instead.
130  
131 =cut
132  
133 sub field {
134 11 ▶ my ($class, $key, $value) = @_;
Subroutine field: called CC=1 SCAR=0.0
Pod documented
135  
136 11 $class->_class_data->{fields}->{$key} = $value;
137  
138 11 return;
139 }
140  
141 =item B<exclude>
142  
143 Sometimes you want some fields to be in the factory but not in the created object.
144  
145 You can use C<exclude> to exclude them. Both arrayref and scalar are accepted.
146  
147 {
148 package My::UserFactory;
149  
150 use base qw(DBIx::Class::Factory);
151  
152 __PACKAGE__->resultset(My::Schema->resultset('User'));
153 __PACKAGE__->exclude('all_names');
154 __PACKAGE__->fields({
155 first_name => __PACKAGE__->callback(sub {shift->get('all_names')}),
156 last_name => __PACKAGE__->callback(sub {shift->get('all_names')}),
157 });
158 }
159  
160 My::UserFactory->create({all_names => 'Bond'});
161  
162 =cut
163  
164 sub exclude {
165 1 ▶ my ($class, $list) = @_;
Subroutine exclude: called CC=3 SCAR=11.0
Pod documented
166  
167 1 ▶ unless (ref $list eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $list eq "ARRAY") 1 0 1
168 1 $list = [$list];
169 }
170  
171 1 foreach my $exclude_field (@{$list}) {
172 1 $class->_class_data->{exclude}->{$exclude_field} = 1;
173 }
174  
175 1 return;
176 }
177  
178 =back
179  
180 =head2 Helpers
181  
182 Sometimes you want the value of the field to be not just static value but something special.
183 Helpers are here for that.
184  
185 =over
186  
187 =item B<callback>
188  
189 Sometimes you want field value to be calculated everytime fields for object are created.
190 Just provide C<callback> as a value in that case.
191  
192 It will be called with the L<DBIx::Class::Factory::Fields> instance as an argument.
193  
194 __PACKAGE__->fields({
195 status => __PACKAGE__->callback(sub {
196 my ($fields) = @_;
197
198 return $fields->get('superuser') ? 3 : 5;
199 }),
200 });
201  
202 =cut
203  
204 sub callback {
205 2 ▶ my ($class, $block) = @_;
Subroutine callback: called CC=1 SCAR=0.0
Pod documented
206  
207 return sub {
208 2 ▶ $block->(@_);
Subroutine __ANON__: called CC=1 SCAR=0.0
209 }
210 2 }
211  
212 =item B<seq>
213  
214 Same as L</callback>, but the callback is called with an additional first argument: the iterating counter.
215  
216 You can also provide the initial value of the counter (C<0> is default).
217  
218 __PACKAGE__->field(id => __PACKAGE__->seq(sub {shift}, 1));
219  
220 =cut
221  
222 sub seq {
223 1 ▶ my ($class, $block, $init_value) = @_;
Subroutine seq: called CC=2 SCAR=7.0
Pod documented
224  
225 1 ▶ $init_value = 0 unless defined $init_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $init_value 0 1 1
226  
227 return sub {
228 13 ▶ $block->($init_value++, @_);
Subroutine __ANON__: called CC=1 SCAR=0.0
229 }
230 1 }
231  
232 =item B<related_factory>
233  
234 This helper just calls another factory's L</get_fields> method.
235 Thanks to C<DBIx::Class>, the returned data will be used to create a related object.
236  
237 package My::UserFactory;
238  
239 use base qw(DBIx::Class::Factory);
240  
241 __PACKAGE__->resultset(My::Schema->resultset('User'));
242 __PACKAGE__->fields({
243 # create a new city if it's not specified
244 city => __PACKAGE__->related_factory('My::CityFactory'),
245 });
246  
247 =cut
248  
249 sub related_factory {
250 1 ▶ my ($class, $factory_class, $extra_fields) = @_;
Subroutine related_factory: called CC=1 SCAR=0.0
Pod documented
251  
252 return sub {
253 1 ▶ return $factory_class->get_fields($extra_fields);
Subroutine __ANON__: called CC=1 SCAR=0.0
254 1 };
255 }
256  
257 =item B<related_factory_batch>
258  
259 Same as L</related_factory>, but calls L</get_fields_batch> method.
260  
261 __PACKAGE__->fields({
262 # Add three accounts to the user
263 accounts => __PACKAGE__->related_factory_batch(3, 'My::AccountFactory')
264 });
265  
266 =cut
267  
268 sub related_factory_batch {
269 1 ▶ my ($class, $n, $factory_class, $extra_fields) = @_;
Subroutine related_factory_batch: called CC=1 SCAR=0.0
Pod documented
270  
271 return sub {
272 1 ▶ return $factory_class->get_fields_batch($n, $extra_fields);
Subroutine __ANON__: called CC=1 SCAR=0.0
273 1 };
274 }
275  
276 =back
277  
278 =head2 Factory actions
279  
280 =over
281  
282 =item B<get_fields>
283  
284 Returns fields that will be used to create object without creating something.
285  
286 =cut
287  
288 sub get_fields {
289 19 ▶ my ($class, $extra_fields) = @_;
Subroutine get_fields: called CC=2 SCAR=6.9
Pod documented
290  
291 19 ▶ $extra_fields = {} unless defined $extra_fields;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extra_fields 10 9 19
292  
293 my $fields = DBIx::Class::Factory::Fields->new(
294 {
295 19 %{$class->_class_data->{fields}},
296 19 %{$extra_fields},
297 },
298 $class->_class_data->{exclude}
299 19 );
300  
301 19 return $class->after_get_fields($fields->all());
302 }
303  
304 =item B<build>
305  
306 Creates L<DBIx::Class::Row> object without saving it to a database.
307  
308 =cut
309  
310 sub build {
311 13 ▶ my ($class, $extra_fields) = @_;
Subroutine build: called CC=2 SCAR=7.0
Pod documented
312  
313 13 my $resultset = $class->_class_data->{resultset};
314 13 ▶ die unless defined $resultset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $resultset 0 13 13
315  
316 13 return $class->after_build($resultset->new($class->get_fields($extra_fields)));
317 }
318  
319 =item B<create>
320  
321 Creates L<DBIx::Class::Row> object and saves it to a database.
322  
323 You can also provide the second argument, hashref of options.
324 The only option is C<discard_changes>, a boolean value which default is true.
325 It says whether L<DBIx::Class::Row/discard_changes> should be called on the created object.
326  
327 My::UserFactory->create(undef, {discard_changes => 0});
328  
329 =cut
330  
331 sub create {
332 10 ▶ my ($class, $extra_fields, $options) = @_;
Subroutine create: called CC=4 SCAR=13.9
Pod documented
333  
334 10 ▶ $options = {} unless defined $options;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options 9 1 10
335 10 ▶ $options->{discard_changes} = 1 unless exists $options->{discard_changes};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"discard_changes"} 9 1 10
336  
337 10 my $row = $class->build($extra_fields)->insert();
338 10 ▶ $row->discard_changes if $options->{discard_changes};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"discard_changes"} 9 1 10
339  
340 10 return $class->after_create($row);
341 }
342  
343 =item B<get_fields_batch>
344  
345 Runs L</get_fields> C<n> times and returns arrayref of results.
346  
347 =cut
348  
349 sub get_fields_batch {
350 2 ▶ my ($class, @params) = @_;
Subroutine get_fields_batch: called CC=1 SCAR=0.0
Pod documented
351  
352 2 return $class->_batch('get_fields', @params);
353 }
354  
355 =item B<build_batch>
356  
357 Runs L</build> C<n> times and returns arrayref of results.
358  
359 =cut
360  
361 sub build_batch {
362 1 ▶ my ($class, @params) = @_;
Subroutine build_batch: called CC=1 SCAR=0.0
Pod documented
363  
364 1 return $class->_batch('build', @params);
365 }
366  
367 =item B<create_batch>
368  
369 Runs L</create> C<n> times and returns arrayref of results.
370  
371 =cut
372  
373 sub create_batch {
374 1 ▶ my ($class, @params) = @_;
Subroutine create_batch: called CC=1 SCAR=0.0
Pod documented
375  
376 1 return $class->_batch('create', @params);
377 }
378  
379 =back
380  
381 =head2 Hooks
382  
383 You can define the following methods in your factory to be executed after corresponding methods.
384  
385 They take result of the corresponding methods as an argument and must return the new one.
386  
387 =over
388  
389 =item B<after_get_fields>
390 =cut
391  
392 sub after_get_fields {
393 18 ▶ my ($class, $fields) = @_;
Subroutine after_get_fields: called CC=1 SCAR=0.0
Pod documented
394  
395 18 return $fields;
396 }
397  
398 =item B<after_build>
399 =cut
400  
401 sub after_build {
402 12 ▶ my ($class, $row) = @_;
Subroutine after_build: called CC=1 SCAR=0.0
Pod documented
403  
404 12 return $row;
405 }
406  
407 =item B<after_create>
408  
409 sub after_create {
410 my ($class, $user_row) = @_;
411  
412 $user_row->auth();
413  
414 return $user_row;
415 }
416 =cut
417  
418 sub after_create {
419 9 ▶ my ($class, $row) = @_;
Subroutine after_create: called CC=1 SCAR=0.0
Pod documented
420  
421 9 return $row;
422 }
423  
424 =back
425  
426 =cut
427  
428 # PRIVATE METHODS
429  
430 sub _batch {
431 4 ▶ my ($class, $method, $n, $extra_fields) = @_;
Subroutine _batch: called CC=2 SCAR=6.9
432  
433 4 my @batch = ();
434 4 for (1 .. $n) {
435 8 push(@batch, $class->$method($extra_fields));
436 }
437  
438 4 return \@batch;
439 }
440  
441 sub _class_data {
442 94 ▶ my ($class) = @_;
Subroutine _class_data: called CC=2 SCAR=6.9
443  
444 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
445  
446 94 my $var_name = $class . '::_dbix_class_factory_data';
447  
448 94 ▶ unless (defined ${$var_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined ${$var_name;}) 7 87 94
449 7 ${$var_name} = {fields => {}};
450 }
451  
452 94 return ${$var_name};
453 }
454  
455 =head1 AUTHOR
456  
457 Vadim Pushtaev, C<pushtaev@cpan.org>
458  
459 =head1 BUGS AND FEATURES
460  
461 Bugs are possible, feature requests are welcome. Write me as soon as possible.
462  
463 =head1 LICENSE AND COPYRIGHT
464  
465 Copyright 2015 Vadim Pushtaev.
466  
467 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
468  
469 =cut
470  
471 1 1;
« Factory/Fields.pm ↑ Summary