| 1 | package OpenERP::OOM::DynamicUtils; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | OpenERP::OOM::DynamicUtils | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | with 'OpenERP::OOM::DynamicUtils'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | ... | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | $self->ensure_class_loaded($class); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | ... | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | $object_data->{$attribute->name} = $self->prepare_attribute_for_send($attribute->type_constraint, $object_data->{$attribute->name}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | This role provides a couple of common methods for our OpenERP base classes. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | It's name is a bit of a misnomer because it just contains a couple of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | useful functions, rather than a clear separation of concerns. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | =head2 ensure_class_loaded | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | This method is designed to ensure we have effectively 'use'd | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | the class while ensuring we don't keep reloading it. It is effectively | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | based on code seen in DBIx::Class and various other projects. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | =head2 prepare_attribute_for_send | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | This converts dates to strings for sending and wraps up strings in RPC::XML::string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | objects to prevent numbers from being transmitted as the wrong type. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | Copyright (C) 2011 OpusVL | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 3 | ▶ | use Class::Inspector; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | ▶ | use RPC::XML qw<RPC_BOOLEAN>; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 3 | my $invalid_class = qr/(?: \b:\b | \:{3,} | \:\:$ )/x; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | sub ensure_class_loaded | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ensure_class_loaded: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | return if Class::Inspector->loaded($class); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $file = Class::Inspector->filename($class); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | Carp::croak "Unable to find class $class" unless $file; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # code stolen from Class::C3::Componentised ensure_class_loaded | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | eval { local $_; require($file) } or do { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $@ = "Invalid class name '$class'" if $class =~ $invalid_class; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ($self->can('throw_exception')) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | $self->throw_exception($@); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | Carp::croak $@; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | sub prepare_attribute_for_send | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_attribute_for_send: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $type = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $value = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if (!defined $value) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | { return RPC_BOOLEAN(0); } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | elsif ($type =~ /Str/i) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | { return RPC::XML::string->new($value); } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ▶ | elsif ($type =~ qr'DateTime'i && $value && ref $value && $value->can('ymd')) | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/9
$type =~ qr/DateTime/i and $value and ref $value and $value->can("ymd")
$type =~ qr/DateTime/i and $value and ref $value
$type =~ qr/DateTime/i and $value
MC/DC0%0/4
$type =~ qr/DateTime/i and $value and ref $value and $value->can("ymd")
$type =~ qr/DateTime/i
$value
ref $value
$value->can("ymd")
Truth table0%0/5
A and B and C and D
A= $type =~ qr/DateTime/iB= $valueC= ref $valueD= $value->can("ymd")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | { return $value->ymd; } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # ^ TODO that only the date part matters is a terrible assumption to make | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | { return $value; } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||