File Coverage

blib/lib/OpenERP/OOM/DynamicUtils.pm
Criterion Covered Total %
statement 9 29 31.0
branch 0 16 0.0
condition 0 12 0.0
subroutine 3 5 60.0
pod 2 2 100.0
total 14 64 21.8


line stmt bran cond sub pod time code
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     3   3545 use Class::Inspector;
  3         2908  
  3         89  
47 3     3   1109 use Moose::Role;
  3         13379  
  3         11  
48 3     3   14749 use Carp ();
  3         7  
  3         880  
49              
50             my $invalid_class = qr/(?: \b:\b | \:{3,} | \:\:$ )/x;
51              
52             sub ensure_class_loaded
53             {
54 0     0 1   my $self = shift;
55 0           my $class = shift;
56 0 0         return if Class::Inspector->loaded($class);
57              
58 0           my $file = Class::Inspector->filename($class);
59 0 0         Carp::croak "Unable to find class $class" unless $file;
60             # code stolen from Class::C3::Componentised ensure_class_loaded
61 0 0         eval { local $_; require($file) } or do {
  0            
  0            
62              
63 0 0         $@ = "Invalid class name '$class'" if $class =~ $invalid_class;
64              
65 0 0         if ($self->can('throw_exception')) {
66 0           $self->throw_exception($@);
67             } else {
68 0           Carp::croak $@;
69             }
70             };
71              
72 0           return;
73             }
74              
75             sub prepare_attribute_for_send
76             {
77 0     0 1   my $self = shift;
78 0           my $type = shift;
79 0           my $value = shift;
80              
81 0 0 0       return RPC::XML::string->new($value) if $type =~ /Str/i && defined $value;
82 0 0         return RPC::XML::boolean->new($value) if $type =~ /Str/i; # return null in effect
83 0 0 0       return $value->ymd if $type =~ qr'DateTime'i && $value && ref $value && $value->can('ymd');
      0        
      0        
84            
85 0           return $value;
86             }
87              
88             1;
89