File Coverage

blib/lib/OpenERP/OOM/DynamicUtils.pm
Criterion Covered Total %
statement 12 33 36.3
branch 0 16 0.0
condition 0 9 0.0
subroutine 4 6 66.6
pod 2 2 100.0
total 18 66 27.2


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