PopulateMore/Command.pm

filter: Statementstmt 96.1% 75 / 78 Branchbran 75.0% 6 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 21 / 21 Podpod 100% 6 / 6 totaltotal 92.3% 109 / 118 CC 8 SCAR 5.6
CC
8
Cov
92%
field_value
3.8
execute
3.0
dispatch_inflator
2.2
CRAP 1.7
« PopulateMore/Inflator/Index.pm ↑ Summary PopulateMore/Inflator/Date.pm »
1 package DBIx::Class::Schema::PopulateMore::Command;
2  
3 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use List::MoreUtils qw(pairwise);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use DBIx::Class::Schema::PopulateMore::Visitor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Type::Library -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 DBIx::Class::Schema::PopulateMore::Command - Command Class to Populate a Schema
15  
16 =head1 DESCRIPTION
17  
18 This is a command pattern class to manage the job of populating a
19 L<DBIx::Class::Schema> with information. We break this out because the
20 actual job is a bit complex, is likely to grow more complex, and so that
21 we can more easily identify refactorable and reusable parts.
22  
23 =head1 ATTRIBUTES
24  
25 This class defines the following attributes.
26  
27 =head2 schema
28  
29 This is the Schema we are populating
30  
31 =cut
32  
33 3 has schema => (
34 is=>'ro',
35 required=>1,
36 isa=>Object,
37 );
38  
39 =head2 exception_cb
40  
41 contains a callback to the exception method supplied by DBIC
42  
43 =cut
44  
45 3 has exception_cb => (
46 is=>'ro',
47 required=>1,
48 isa=>CodeRef,
49 );
50  
51 =head2 definitions
52  
53 This is an arrayref of information used to populate tables in the database
54  
55 =cut
56  
57 3 has definitions => (
58 is=>'ro',
59 required=>1,
60 isa=>ArrayRef[HashRef],
61 );
62  
63  
64 =head2 match_condition
65  
66 How we know the value is really something to inflate or perform a substitution
67 on. This get's the namespace of the substitution plugin and it's other data.
68  
69 =cut
70  
71 has match_condition => (
72 is=>'ro',
73 required=>1,
74 isa=>RegexpRef,
75 5 ▶ default=>sub {qr/^!(\w+:.+)$/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 3 );
77  
78  
79 =head2 visitor
80  
81 We define a visitor so that we can perform the value inflations and or
82 substitutions. This is still a little work in progress, but it's getting
83 neater
84  
85 =cut
86  
87 3 has visitor => (
88 is=>'lazy',
89 isa=>InstanceOf['DBIx::Class::Schema::PopulateMore::Visitor'],
90 handles => [
91 'callback',
92 'visit',
93 ],
94 );
95  
96  
97 =head2 rs_index
98  
99 The index of previously inflated resultsets. Basically when we create a new
100 row in the table, we cache the result object so that it can be used as a
101 dependency in creating another.
102  
103 Eventually will be moved into the constructor for a plugin
104  
105 =head2 set_rs_index
106  
107 Set an index value to an inflated result
108  
109 =head2 get_rs_index
110  
111 given an index, returns the related inflated resultset
112  
113 =cut
114  
115 has rs_index => (
116 is=>'rw',
117 handles_via=>'Hash',
118 isa=>HashRef[Object],
119 5 ▶ default=>sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
120 3 handles=> {
121 set_rs_index => 'set',
122 get_rs_index => 'get',
123 },
124 );
125  
126  
127 =head2 inflator_loader
128  
129 Loads each of the available inflators, provider access to the objects
130  
131 =cut
132  
133 3 has inflator_loader => (
134 is=>'lazy',
135 isa=>InstanceOf['Module::Pluggable::Object'],
136 handles=>{
137 'inflators' => 'plugins',
138 },
139 );
140  
141  
142 =head2 inflator_dispatcher
143  
144 Holds an object that can perform dispatching to the inflators.
145  
146 =cut
147  
148 3 has inflator_dispatcher => (
149 is=>'lazy',
150 handles_via=>'Hash',
151 isa=>HashRef[Object],
152 handles=>{
153 inflator_list => 'keys',
154 get_inflator => 'get',
155 },
156 );
157  
158  
159 =head1 METHODS
160  
161 This module defines the following methods.
162  
163 =head2 _build_visitor
164  
165 lazy build for the L</visitor> attribute.
166  
167 =cut
168  
169 sub _build_visitor
170 {
171 5 ▶ my $self = shift @_;
Subroutine _build_visitor: called CC=1 SCAR=0.0
172
173 5 DBIx::Class::Schema::PopulateMore::Visitor->new({
174 match_condition=>$self->match_condition
175 });
176 }
177  
178  
179 =head2 _build_inflator_loader
180  
181 lazy build for the L</inflator_loader> attribute
182  
183 =cut
184  
185 sub _build_inflator_loader
186 {
187 5 ▶ my $self = shift @_;
Subroutine _build_inflator_loader: called CC=1 SCAR=0.0
188
189 5 return Module::Pluggable::Object->new(
190 search_path => 'DBIx::Class::Schema::PopulateMore::Inflator',
191 require => 1,
192 except => 'DBIx::Class::Schema::PopulateMore::Inflator',
193 );
194 }
195  
196  
197 =head2 _build_inflator_dispatcher
198  
199 lazy build for the L</inflator_dispatcher> attribute
200  
201 =cut
202  
203 sub _build_inflator_dispatcher
204 {
205 5 ▶ my $self = shift @_;
Subroutine _build_inflator_dispatcher: called CC=2 SCAR=6.9
206
207 5 my %inflators;
208 5 for my $inflator ($self->inflators)
209 {
210 20 my $inflator_obj = $inflator->new;
211 20 my $name = $inflator_obj->name;
212
213 20 $inflators{$name} = $inflator_obj;
214
215 }
216
217 5 return \%inflators;
218 }
219  
220  
221 =head2 execute
222  
223 The command classes main method. Returns a Hash of the created result
224 rows, where each key is the named index and the value is the row object.
225  
226 =cut
227  
228 sub execute
229 {
230 5 ▶ my ($self) = @_;
Subroutine execute: called CC=3 SCAR=11.0
Pod documented
231  
232 5 foreach my $definition (@{$self->definitions})
233 {
234 16 my ($source => $info) = %$definition;
235 16 my @fields = $self->coerce_to_array($info->{fields});
236
237 my $data = $self
238 ->callback(sub {
239 33 ▶ $self->dispatch_inflator(shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
240 })
241 16 ->visit($info->{data});
242
243 16 ▶ while( my ($rs_key, $values) = each %{$data} )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$data;}) 24 16 40
244 {
245 24 my @values = $self->coerce_to_array($values);
246
247 24 my $new = $self->create_fixture(
248 $rs_key => $source,
249 $self->merge_fields_values([@fields], [@values])
250 );
251 }
252 }
253
254 5 return %{$self->rs_index};
255 }
256  
257  
258 =head2 dispatch_inflator
259  
260 Dispatch to the correct inflator
261  
262 =cut
263  
264 sub dispatch_inflator
265 {
266 33 ▶ my ($self, $arg) = @_;
Subroutine dispatch_inflator: called CC=2 SCAR=7.9
Pod documented
267 33 my ($name, $command) = ($arg =~m/^(\w+):(\w.+)$/);
268
269 33 ▶ if( my $inflator = $self->get_inflator($name) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $inflator = $self->get_inflator($name)) { } 33 0 33
270 {
271 33 $inflator->inflate($self, $command);
272 }
273 else
274 {
275 0 my $available = join(', ', $self->inflator_list);
276 0 $self->exception_cb->("Can't Handle $name, available are: $available");
277 }
278 }
279  
280  
281 =head2 create_fixture({})
282  
283 Given a hash suitable for a L<DBIx::Class::Resultset> create method, attempt to
284 update or create a row in the named source.
285  
286 returns the newly created row or throws an exception if there is a failure
287  
288 =cut
289  
290 sub create_fixture
291 {
292 24 ▶ my ($self, $rs_key => $source, @create) = @_;
Subroutine create_fixture: called CC=1 SCAR=0.0
Pod documented
293
294 24 my $new = $self
295 ->schema
296 ->resultset($source)
297 ->update_or_create({@create});
298
299 24 $self->set_rs_index("$source.$rs_key" => $new);
300
301 24 return $new;
302 }
303  
304  
305 =head2 merge_fields_values
306  
307 Given a fields and values, combine to a hash suitable for using in a create_fixture
308 row statement.
309  
310 =cut
311  
312 sub merge_fields_values
313 {
314 24 ▶ my ($self, $fields, $values) = @_;
Subroutine merge_fields_values: called CC=1 SCAR=0.0
Pod documented
315
316 return pairwise {
317 58 ▶ $self->field_value($a,$b)
Subroutine __ANON__: called CC=1 SCAR=0.0
318 24 } (@$fields, @$values);
319 }
320  
321  
322 =head2 field_value
323  
324 Correctly create an array from the fields, values variables, skipping those
325 where the value is undefined.
326  
327 =cut
328  
329 sub field_value
330 {
331 58 ▶ my ($self, $a, $b) = @_;
Subroutine field_value: called CC=3 SCAR=13.3
Pod documented
332
333 58 ▶ if(defined $a && defined $b)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a and defined $b) { } 58 0 58
Condition33%1/3
defined $a and defined $b
!ll&&!rl&&r
0058
MC/DC0%0/2
defined $a and defined $b
defined $a defined $b
Truth table33%1/3
A and B
A= defined $aB= defined $b
ABresult
0X0
100
111
334 {
335 58 return $a => $b;
336 }
337 else
338 {
339 0 return;
340 }
341 }
342  
343  
344 =head2 coerce_to_array
345  
346 given a value that is either an arrayref or a scalar, put it into array context
347 and return that array.
348  
349 =cut
350  
351 sub coerce_to_array
352 {
353 40 ▶ my ($self, $value) = @_;
Subroutine coerce_to_array: called CC=2 SCAR=6.9
Pod documented
354
355 40 ▶ return ref $value eq 'ARRAY' ? @$value:($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 32 8 40
356 }
357  
358  
359 =head1 AUTHOR
360  
361 Please see L<DBIx::Class::Schema::PopulateMore> For authorship information
362  
363 =head1 LICENSE
364  
365 Please see L<DBIx::Class::Schema::PopulateMore> For licensing terms.
366  
367 =cut
368  
369  
370 3 1;
« PopulateMore/Inflator/Index.pm ↑ Summary PopulateMore/Inflator/Date.pm »