OOM/Class/Base.pm

filter: Statementstmt 19.6% 31 / 158 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 36.0% 9 / 25 Podpod 100% 11 / 11 totaltotal 19.2% 51 / 265 CC 40 SCAR 43.2
CC
40
Cov
14%
_collapse_data_to_ids
210.0
raw_search
130.7
retrieve_list
20.0
CRAP 75.3
↑ Summary OOM/Object/Base.pm »
1 package OpenERP::OOM::Class::Base;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use RPC::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use MooseX::NotRequired;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 extends 'Moose::Object';
13 2 with 'OpenERP::OOM::DynamicUtils';
14  
15 =head1 NAME
16  
17 OpenERP::OOM::Class::Base
18  
19 =head1 SYNOPSYS
20  
21 my $obj = $schema->class('Name')->create(\%args);
22
23 foreach my $obj ($schema->class('Name')->search(@query)) {
24 ...
25 }
26  
27 =head1 DESCRIPTION
28  
29 Provides a base set of methods for OpenERP::OOM classes (search, create, etc).
30  
31 =cut
32  
33 2 has 'schema' => (
34 is => 'ro',
35 );
36  
37 2 has 'object_class' => (
38 is => 'ro',
39 lazy => 1,
40 builder => '_build_object_class',
41 );
42  
43 sub _build_object_class {
44 0 ▶ my $self = shift;
Subroutine _build_object_class: not called CC=2 SCAR=17.9
45
46 # if you get this blow up it probably means the class doesn't compile for some
47 # reason. Run the t/00-load.t tests. If they pass check you have a use_ok
48 # statement for all your modules.
49 0 ▶ die 'Your code doesn\'t compile llamma' if !$self->can('object');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("object") 0 0 0
50 0 $self->ensure_class_loaded($self->object);
51
52 0 ▶ $self->object->meta->add_method('class' => sub{return $self});
Subroutine __ANON__: not called CC=1 SCAR=6.9
53
54 0 return $self->object->new;
55 }
56  
57 #-------------------------------------------------------------------------------
58  
59 =head2 search
60  
61 Searches OpenERP and returns a list of objects matching a given query.
62  
63 my @list = $schema->class('Name')->search(
64 ['name', 'ilike', 'OpusVL'],
65 ['active', '=', 1],
66 );
67  
68 The query is formatted as a list of array references, each specifying a
69 column name, operator, and value. The objects returned will be those where
70 all of these sub-queries match.
71  
72 Searches can be performed against OpenERP fields, linked objects (e.g. DBIx::Class
73 relationships), or a combination of both.
74  
75 my @list = $schema->class('Name')->search(
76 ['active', '=', 1],
77 ['details', {status => 'value'}, {}],
78 )
79  
80 In this example, 'details' is a linked DBIx::Class object with a column called
81 'status'.
82  
83 An optional 'search context' can also be provided at the end of the query list, e.g.
84  
85 my @list = $schema->class('Location')->search(
86 ['usage' => '=' => 'internal'],
87 ['active' => '=' => 1],
88 {
89 active_id => $self->id,
90 active_ids => [$self->id],
91 active_model => 'product.product',
92 full => 1,
93 product_id => $self->id,
94 search_default_in_location => 1,
95 section_id => undef,
96 tz => undef,
97 }
98 );
99  
100 Supplying a context further restricts the search, for example to narrow down a
101 'stock by location' query to 'stock of a specific product by location'.
102  
103 Following the search context, an arrayref of options can be given to return a
104 paged set of results:
105  
106 {
107 limit => 10, # Return max 10 results
108 offset => 20, # Start at result 20
109 }
110  
111 =head2 raw_search
112  
113 This is the same as search but it doesn't turn the results into objects. This
114 is useful if your search is likely to have returned fields that aren't part of
115 the object. Queries like those used by the Stock By Location report are likely
116 to return stock levels as well as the location details for example.
117  
118 =cut
119  
120 sub raw_search {
121 0 ▶ my ($self, @args) = @_;
Subroutine raw_search: not called CC=12 SCAR=48.7
Pod documented
122 2 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 ### Initial search args: @args
124
125 0 my @search;
126 0 ▶ while (@args && ref $args[0] ne 'HASH') {push @search, shift @args}
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] ne "HASH") 0 0 0
Condition0%0/3
@args and ref $args[0] ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@args and ref $args[0] ne "HASH"
@args ref $args[0] ne "HASH"
Truth table0%0/3
A and B
A= @argsB= ref $args[0] ne "HASH"
ABresult
0X0
100
111
127
128 # Loop through each search criteria, and if it is a linked object
129 # search, replace it with a translated OpenERP search parameter.
130 0 foreach my $criteria (@search) {
131 0 ▶ if(ref $criteria eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $criteria eq "ARRAY") 0 0 0
132 0 my $search_field = $criteria->[0];
133  
134 0 ▶ if (my $link = $self->object_class->meta->link->{$search_field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $link = $self->object_class->meta->link->{$search_field}) 0 0 0
135 0 ▶ if ($self->schema->link($link->{class})->can('search')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->schema->link($link->{'class'})->can('search')) { } 0 0 0
136 0 my @results = $self->schema->link($link->{class})->search($link->{args}, @$criteria[1 .. @$criteria-1]);
137  
138 0 ▶ if (@results) {
Branch: 1 branch
BranchTrueFalseTotal
if (@results) { } 0 0 0
139 ### Adding to OpenERP search:
140 ### $link->{key}
141 ### IN
142 ### join(', ', @results)
143 0 $criteria = [$link->{key}, 'in', \@results];
144 } else {
145 0 return; # No results found, so no point searching in OpenERP
146 }
147 } else {
148 0 carp "Cannot search for link type " . $link->{class};
149 }
150 }
151 }
152 }
153
154 0 my $context = shift @args;
155 0 my $options = shift @args;
156 0 ▶ $options = {} unless $options;
Branch: 1 branch
BranchTrueFalseTotal
unless $options 0 0 0
157 ### Search: @search
158 ### Search context: $context
159 ### Search options: $options
160 0 my $objects = $self->schema->client->search_detail($self->object_class->model,[@search], $context, $options->{offset}, $options->{limit});
161  
162 0 ▶ if ($objects) {
Branch: 1 branch
BranchTrueFalseTotal
if ($objects) { } 0 0 0
163 0 foreach my $attribute ($self->object_class->meta->get_all_attributes) {
164 0 ▶ if($attribute->type_constraint =~ /DateTime/)
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->type_constraint =~ /DateTime/) 0 0 0
165 {
166 0 my $parser = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d');
167 0 map { $_->{$attribute->name} = $parser->parse_datetime($_->{$attribute->name}) } @$objects;
168 }
169 }
170 0 return $objects;
171 } else {
172 0 return undef;
173 }
174 }
175  
176 sub search
177 {
178 0 ▶ my $self = shift;
Subroutine search: not called CC=3 SCAR=24.8
Pod documented
179 0 my $objects = $self->raw_search(@_);
180 0 ▶ if($objects) {
Branch: 1 branch
BranchTrueFalseTotal
if ($objects) { } 0 0 0
181 0 return map {$self->object_class->new($_)} @$objects;
182 } else {
183 0 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
184 }
185 }
186  
187 =head2 is_not_null
188  
189 Returns search criteria for a not null search. i.e. equivalend to $field is not null in SQL.
190  
191 $self->search($self->is_not_null('x_department'), [ 'other_field', '=', 3 ]);
192  
193 =cut
194  
195 sub is_not_null
196 {
197 0 ▶ my $self = shift;
Subroutine is_not_null: not called CC=1 SCAR=6.9
Pod documented
198 0 my $field = shift;
199 0 return [ $field, '!=', RPC::XML::boolean->new(0) ];
200 }
201  
202 =head2 null
203  
204 Returns a 'null' for use in OpenERP calls and objects. (Actually this is a False value).
205  
206 =cut
207  
208 0 ▶sub null { RPC::XML::boolean->new(0) }
Subroutine null: not called CC=1 SCAR=6.9
Pod documented
209  
210 =head2 is_null
211  
212 Returns search criteria for an is null search. i.e. equivalend to $field is null in SQL.
213  
214 $self->search($self->is_null('x_department'), [ 'other_field', '=', 3 ]);
215  
216 =cut
217  
218 sub is_null
219 {
220 0 ▶ my $self = shift;
Subroutine is_null: not called CC=1 SCAR=6.9
Pod documented
221 0 my $field = shift;
222 0 return [ $field, '=', RPC::XML::boolean->new(0) ];
223 }
224  
225 #-------------------------------------------------------------------------------
226  
227 =head2 find
228  
229 Returns the first object matching a given query.
230  
231 my $obj = $schema->class('Name')->find(['id', '=', 32]);
232  
233 Will return C<undef> if no objects matching the query are found.
234  
235 =cut
236  
237 sub find {
238 0 ▶ my $self = shift;
Subroutine find: not called CC=2 SCAR=17.9
Pod documented
239
240 #my $ids = $self->schema->client->search($self->object_class->model,[@_]);
241 0 my $ids = $self->raw_search(@_);
242
243 0 ▶ if ($ids->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ids->[0]) 0 0 0
244 #return $self->retrieve($ids->[0]);
245 0 return $self->object_class->new($ids->[0]);
246 }
247 }
248  
249  
250 =head2 get_options
251  
252 This returns the options for available for a selection field. It will croak if you
253 try to give it a field that isn't an option.
254  
255 =cut
256  
257 sub get_options
258 {
259 0 ▶ my $self = shift;
Subroutine get_options: not called CC=2 SCAR=17.9
Pod documented
260 0 my $field = shift;
261  
262 0 my $model_info = $self->schema->client->model_fields($self->object_class->model);
263 0 my $field_info = $model_info->{$field};
264 0 ▶ croak 'Can only get options for selection objects' unless $field_info->{type} eq 'selection';
Branch: 1 branch
BranchTrueFalseTotal
unless $field_info->{"type"} eq "selection" 0 0 0
265 0 my $options = $field_info->{selection};
266 0 return $options;
267 }
268  
269 #-------------------------------------------------------------------------------
270  
271 =head2 retrieve
272  
273 Returns an object by ID.
274  
275 my $obj = $schema->class('Name')->retrieve(32);
276  
277 =cut
278  
279 sub retrieve {
280 0 ▶ my ($self, $id, @args) = @_;
Subroutine retrieve: not called CC=2 SCAR=17.9
Pod documented
281
282 # FIXME - This should probably be in a try/catch block
283 0 ▶ if (my $object = $self->schema->client->read_single($self->object_class->model, $id, @args))
Branch: 1 branch
BranchTrueFalseTotal
if (my $object = $self->schema->client->read_single($self->object_class->model, $id, @args)) 0 0 0
284 {
285 0 return $self->_inflate_object($self->object, $object);
286 }
287 }
288  
289 sub _inflate_object
290 {
291 0 ▶ my $self = shift;
Subroutine _inflate_object: not called CC=3 SCAR=24.8
292 0 my $object_class = shift;
293 0 my $object = shift;
294  
295 0 foreach my $attribute ($self->object_class->meta->get_all_attributes) {
296 0 ▶ if($attribute->type_constraint =~ /DateTime/)
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->type_constraint =~ /DateTime/) 0 0 0
297 {
298 0 my $parser = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d');
299 0 $object->{$attribute->name} = $parser->parse_datetime($object->{$attribute->name});
300 }
301 }
302 0 return $object_class->new($object);
303 }
304  
305 =head2 default_values
306  
307 Returns an instance of the object filled in with the default values suggested by OpenERP.
308  
309 =cut
310 sub default_values
311 {
312 0 ▶ my $self = shift;
Subroutine default_values: not called CC=1 SCAR=6.9
Pod documented
313 0 my $context = shift;
314 # do a default_get
315  
316 0 my @fields = map { $_->name } $self->object_class->meta->get_all_attributes;
317 0 my $object = $self->schema->client->get_defaults($self->object_class->model, \@fields, $context);
318 0 my $class = MooseX::NotRequired::make_optional_subclass($self->object);
319 0 return $self->_inflate_object($class, $object);
320 }
321  
322 #-------------------------------------------------------------------------------
323  
324 =head2 retrieve_list
325  
326 Takes a reference to a list of object IDs and returns a list of objects.
327  
328 my @list = $schema->class('Name')->retrieve_list([32, 15, 60]);
329  
330 =cut
331  
332 sub retrieve_list {
333 0 ▶ my ($self, $ids, @args) = @_;
Subroutine retrieve_list: not called CC=4 SCAR=30.0
Pod documented
334
335 0 ▶ if (my $objects = $self->schema->client->read($self->object_class->model, $ids, @args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $objects = $self->schema->client->read($self->object_class->model, $ids, @args)) 0 0 0
336 0 foreach my $attribute ($self->object_class->meta->get_all_attributes) {
337 0 ▶ if($attribute->type_constraint =~ /DateTime/)
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->type_constraint =~ /DateTime/) 0 0 0
338 {
339 0 my $parser = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d');
340 0 map { $_->{$attribute->name} = $parser->parse_datetime($_->{$attribute->name}) } @$objects;
341 }
342 }
343 0 return map {$self->object_class->new($_)} @$objects;
344 }
345 }
346  
347  
348 #-------------------------------------------------------------------------------
349  
350 sub _collapse_data_to_ids
351 {
352 0 ▶ my ($self, $object_data) = @_;
Subroutine _collapse_data_to_ids: not called CC=14 SCAR=53.5
353  
354 0 my $relationships = $self->object_class->meta->relationship;
355 0 ▶ while (my ($name, $rel) = each %$relationships) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$relationships) 0 0 0
356 0 ▶ if ($rel->{type} eq 'one2many') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "one2many") 0 0 0
357 0 ▶ if ($object_data->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_data->{$name}) 0 0 0
358 0 $object_data->{$rel->{key}} = $self->_id($rel, $object_data->{$name});
359 0 ▶ delete $object_data->{$name} if $name ne $rel->{key};
Branch: 1 branch
BranchTrueFalseTotal
if $name ne $rel->{"key"} 0 0 0
360 }
361 }
362
363 0 ▶ if ($rel->{type} eq 'many2one') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "many2one") 0 0 0
364 0 ▶ if ($object_data->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_data->{$name}) 0 0 0
365 0 $object_data->{$rel->{key}} = $self->_id($rel, $object_data->{$name});
366 0 ▶ delete $object_data->{$name} if $name ne $rel->{key};
Branch: 1 branch
BranchTrueFalseTotal
if $name ne $rel->{"key"} 0 0 0
367 }
368 }
369 0 ▶ if ($rel->{type} eq 'many2many') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "many2many") 0 0 0
370 0 ▶ if ($object_data->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_data->{$name}) 0 0 0
371 0 my $val = $object_data->{$name};
372 0 my @ids;
373 0 ▶ if(ref $val eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 0 0
374 {
375 # they passed in an arrayref.
376 0 my $objects = $val;
377 0 @ids = map { $self->_id($rel, $_) } @$objects;
378 }
379 else
380 {
381 # assume it's a single object.
382 0 push @ids, $self->_id($rel, $val);
383 }
384 0 $object_data->{$rel->{key}} = [[ 6, 0, \@ids ]];
385 0 ▶ delete $object_data->{$name} if $name ne $rel->{key};
Branch: 1 branch
BranchTrueFalseTotal
if $name ne $rel->{"key"} 0 0 0
386 }
387 }
388 }
389 # Force Str parameters to be object type RPC::XML::string
390 0 foreach my $attribute ($self->object_class->meta->get_all_attributes) {
391 0 ▶ if (exists $object_data->{$attribute->name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$object_data{$attribute->name}) 0 0 0
392 0 $object_data->{$attribute->name} = $self->prepare_attribute_for_send($attribute->type_constraint, $object_data->{$attribute->name});
393 }
394 }
395 0 return $object_data;
396 }
397  
398 sub _id
399 {
400 0 ▶ my $self = shift;
Subroutine _id: not called CC=4 SCAR=30.0
401 0 my $rel = shift;
402 0 my $val = shift;
403 0 my $ref = ref $val;
404 0 ▶ if($ref)
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 0 0 0
405 {
406 # FIXME: this is close to what I want but I need to be doing it with the class
407 # that corresponds to the relation we're delving into.
408 0 ▶ if($ref eq 'HASH')
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
409 {
410 0 my $class = $self->schema->class($rel->{class});
411 0 return [[ 0, 0, $class->_collapse_data_to_ids($val) ]];
412 }
413 elsif($ref eq 'ARRAY')
414 {
415 # this should allow us to do child objects too.
416 0 my $class = $self->schema->class($rel->{class});
417 0 my @expanded = map { [ 0, 0, $class->_collapse_data_to_ids($_) ] } @$val;
418 0 return \@expanded;
419 }
420 else
421 {
422 0 return $val->id;
423 }
424 }
425 0 return $val;
426 }
427  
428 =head2 create
429  
430 Creates a new instance of an object in OpenERP.
431  
432 my $obj = $schema->class('Name')->create({
433 name => 'OpusVL',
434 active => 1,
435 });
436  
437 Takes a hashref of object parameters.
438  
439 Returns the new object or C<undef> if it could not be created.
440  
441 =cut
442  
443 sub create {
444 0 ▶ my ($self, $object_data, @args) = @_;
Subroutine create: not called CC=2 SCAR=17.9
Pod documented
445  
446 ### Create called with initial object data:
447 ### $object_data;
448
449 0 $object_data = $self->_collapse_data_to_ids($object_data);
450  
451 ### To
452 ### $object_data;
453
454 0 ▶ if (my $id = $self->schema->client->create($self->object_class->model, $object_data, @args))
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $self->schema->client->create($self->object_class->model, $object_data, @args)) 0 0 0
455 {
456 0 return $self->retrieve($id);
457 }
458 }
459  
460  
461 #-------------------------------------------------------------------------------
462  
463 =head1 AUTHOR
464  
465 Jon Allen (JJ) - L<jj@opusvl.com>
466  
467 =head1 COPYRIGHT and LICENSE
468  
469 Copyright (C) 2010 Opus Vision Limited
470  
471 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
472  
473 =cut
474  
475 2 1;
↑ Summary OOM/Object/Base.pm »