OOM/Object.pm

filter: Statementstmt 29.5% 26 / 88 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 36.8% 7 / 19 Podpod 100% 4 / 4 totaltotal 28.2% 37 / 131 CC 11 SCAR 27.2
CC
11
Cov
24%
__ANON__
42.0
__ANON__
30.0
_id
6.0
CRAP 15.1
« OOM/Object/Base.pm ↑ Summary OOM/Roles/Attribute.pm »
1 package OpenERP::OOM::Object;
2  
3 =head1 NAME
4  
5 OpenERP::OOM::Object
6  
7 =head1 SYNOPSIS
8  
9 package Package::OpenERP::Object::Account;
10  
11 use 5.010;
12 use OpenERP::OOM::Object;
13  
14 openerp_model 'account.account';
15  
16 has active => (is => 'rw', isa => 'Bool'); # Active
17 has code => (is => 'rw', isa => 'Str'); # (required) Code
18  
19 ...
20  
21 relationship 'consolidated_children' => (
22 key => 'child_consol_ids',
23 type => 'many2many',
24 class => 'Account',
25 ); # Consolidated Children
26  
27 ...
28  
29 1;
30  
31 =head1 DESCRIPTION
32  
33 Use this module to create the 'objects' for your models. It also implicitly loads
34 Moose too.
35  
36 The class is linked to a model in OpenERP.
37  
38 =head1 METHODS
39  
40 =head2 openerp_model
41  
42 Specify the model in OpenERP.
43  
44 =head2 init_meta
45  
46 An internal method that hooks up the Moose internals and implicitly makes your
47 new classes inherit from OpenERP::OOM::Object::Base. See the
48 OpenERP::OOM::Object::Base documentation for a list of the methods your objects
49 will have by default.
50  
51 =head2 relationship
52  
53 Used to specify relationships between this object and others in OpenERP.
54  
55 Possible options for the type are many2one, one2many and many2many. These
56 are specified in OpenERP in those terms.
57  
58 =head2 has_link
59  
60 Used to indicate links with other systems. Typically this is to another table
61 in DBIC at the moment.
62  
63 The key field is in OpenERP and is used for the ids of the objects to link to.
64  
65 The class specifies a link class that is used to follow the link. These are in
66 the namespace OpenERP::OOM::Link. When class is set to DBIC this means it loads
67 OpenERP::OOM::Link::DBIC to follow the link to the DBIC rows.
68  
69 Possible options for type are C<single> and C<multiple>.
70  
71 has_link 'details' => (
72 key => 'x_dbic_link_id',
73 type => 'single',
74 class => 'DBIC',
75 args => {class => 'AuctionHouseDetails'},
76 );
77  
78 =head1 COPYRIGHT & LICENSE
79  
80 Copyright (C) 2011 OpusVL
81  
82 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
83  
84 =cut
85  
86 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 2 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 2 ▶use Moose::Util::MetaRole;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 #-------------------------------------------------------------------------------
94  
95 # Set up a subtype for many2one relationships. On object retrieval, OpenERP
96 # presents this relationship as an array reference holding the ID and the name
97 # of the related object, e.g.
98 #
99 # [ 1, 'Related object name' ]
100 #
101 # However, when updating the object OpenERP expects this to be presented back
102 # as a single integer containing the related object ID.
103  
104 2 subtype 'OpenERP::OOM::Type::Many2One'
105 => as 'Maybe[Int]';
106  
107 coerce 'OpenERP::OOM::Type::Many2One'
108 => from 'ArrayRef'
109 0 ▶ => via { $_->[0] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
110
111  
112 #-------------------------------------------------------------------------------
113  
114 # Export the 'openerp_model' and 'relationship' methods
115  
116 2 Moose::Exporter->setup_import_methods(
117 with_meta => ['openerp_model', 'relationship', 'has_link'],
118 also => 'Moose',
119 );
120  
121  
122 #-------------------------------------------------------------------------------
123  
124 sub init_meta {
125 1 ▶ shift;
Subroutine init_meta: called CC=1 SCAR=0.0
Pod documented
126 1 my %args = @_;
127
128 1 Moose->init_meta( %args, base_class => 'OpenERP::OOM::Object::Base' );
129
130 Moose::Util::MetaRole::apply_metaroles(
131 for => $args{for_class},
132 1 class_metaroles => {
133 class => [
134 'OpenERP::OOM::Meta::Class::Trait::HasRelationship',
135 'OpenERP::OOM::Meta::Class::Trait::HasLink',
136 ],
137 attribute => ['OpenERP::OOM::Roles::Attribute'],
138 },
139 );
140  
141 Moose::Util::MetaRole::apply_base_class_roles(
142 for_class => $args{for_class},
143 1 roles => ['OpenERP::OOM::Roles::Class'],
144 );
145  
146 }
147  
148  
149 #-------------------------------------------------------------------------------
150  
151 sub openerp_model {
152 0 ▶ my ($meta, $name, %options) = @_;
Subroutine openerp_model: not called CC=1 SCAR=6.9
Pod documented
153
154 $meta->add_method(
155 'model',
156 0 ▶ sub {return $name},
Subroutine __ANON__: not called CC=1 SCAR=6.9
157 0 );
158 }
159  
160  
161 #-------------------------------------------------------------------------------
162  
163 sub relationship {
164 0 ▶ my ($meta, $name, %options) = @_;
Subroutine relationship: not called CC=1 SCAR=6.9
Pod documented
165
166 #carp "Adding relationship $name";
167
168 $meta->relationship({
169 0 %{$meta->relationship},
170 $name => \%options
171 });
172
173 #say "Adding hooks";
174
175 0 given ($options{type}) {
176 0 when ('many2one') {
177 0 goto &_add_rel2one;
178 }
179 0 when ('one2many') {
180 0 goto &_add_rel2many;
181 }
182 0 when ('many2many') {
183 0 goto &_add_rel2many;
184 }
185 }
186 }
187  
188  
189 #-------------------------------------------------------------------------------
190  
191 sub _add_rel2many {
192 0 ▶ my ($meta, $name, %options) = @_;
Subroutine _add_rel2many: not called CC=1 SCAR=6.9
193
194 $meta->add_attribute(
195 $options{key},
196 0 isa => 'ArrayRef',
197 is => 'rw',
198 );
199
200 $meta->add_method(
201 $name,
202 sub {
203 0 ▶ my ($self, @args) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
204 0 my $field_name = $options{key};
205 0 ▶ if(@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
206 {
207 0 my @ids;
208 0 ▶ if(ref $args[0] eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq 'ARRAY') { } 0 0 0
209 {
210 # they passed in an arrayref.
211 # i.e. $obj->rel([ $obj1, $obj2 ]);
212 0 my $objects = $args[0];
213 0 @ids = map { _id($_) } @$objects;
214 }
215 else
216 {
217 # assume they passed each object in as an arg
218 # i.e. $obj->rel($obj1, $obj2);
219 0 @ids = map { _id($_) } @args;
220 }
221 0 $self->$field_name(\@ids);
222 0 ▶ return unless defined wantarray; # avoid needless retrieval
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
223 }
224 0 ▶ return unless $self->{$field_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$field_name} 0 0 0
225 0 return $self->class->schema->class($options{class})->retrieve_list($self->{$field_name});
226 },
227 0 );
228 }
229  
230  
231 # this method means the user can simply pass in id's as well as
232 # objects.
233 sub _id
234 {
235 0 ▶ my $var = shift;
Subroutine _id: not called CC=2 SCAR=17.9
236 0 ▶ return ref $var ? $var->id : $var;
Branch: 1 branch
BranchTrueFalseTotal
ref $var ? : 0 0 0
237 }
238  
239  
240 #-------------------------------------------------------------------------------
241  
242 sub _add_rel2one {
243 0 ▶ my ($meta, $name, %options) = @_;
Subroutine _add_rel2one: not called CC=1 SCAR=6.9
244  
245 0 my $field_name = $options{key};
246 0 $meta->add_attribute(
247 $field_name,
248 isa => 'OpenERP::OOM::Type::Many2One',
249 is => 'rw',
250 coerce => 1,
251 );
252
253 0 my $cache_field = '__cache_' . $field_name;
254 0 $meta->add_attribute(
255 $cache_field,
256 is => 'rw',
257 );
258
259 $meta->add_method(
260 $name,
261 sub {
262 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=6 SCAR=37.4
263 0 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
264 {
265 0 my $val = shift;
266 0 ▶ $self->$field_name($val ? _id($val) : undef);
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 0 0
267 0 $self->$cache_field(undef);
268 0 ▶ return unless defined wantarray; # avoid needless retrieval
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
269 }
270 0 ▶ return unless $self->{$options{key}};
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{$options{"key"}} 0 0 0
271 0 ▶ return $self->$cache_field if defined $self->$cache_field;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->$cache_field 0 0 0
272 0 my $val = $self->class->schema->class($options{class})->retrieve($self->{$options{key}});
273 0 $self->$cache_field($val);
274 0 return $val;
275 },
276 0 );
277 }
278  
279  
280 #-------------------------------------------------------------------------------
281  
282 sub has_link {
283 0 ▶ my ($meta, $name, %options) = @_;
Subroutine has_link: not called CC=1 SCAR=6.9
Pod documented
284
285 $meta->link({
286 0 %{$meta->link},
287 $name => \%options
288 });
289
290 0 given ($options{type}) {
291 0 when ('single') {
292 0 goto &_add_link_single;
293 }
294 0 when ('multiple') {
295 0 goto &_add_link_multiple;
296 }
297 }
298 }
299  
300  
301 #-------------------------------------------------------------------------------
302  
303 sub _add_link_single {
304 0 ▶ my ($meta, $name, %options) = @_;
Subroutine _add_link_single: not called CC=1 SCAR=6.9
305
306 $meta->add_attribute(
307 $options{key},
308 0 isa => 'Int',
309 is => 'ro',
310 );
311
312 #$meta->add_method(
313 # $name,
314 # sub {
315 # my $self = shift;
316 #
317 # # FIXME - this is a bit broken, remembers between different objects!
318 # # Looks like the method is being added to the whole class, not the individual object
319 # # Maybe this code needs to go in Base.pm instead, in an "around create{...}" block
320 # # to add these methods in when a new object is created.
321 # state $link = $self->class->schema->link($options{class})->retrieve($options{args}, $self->{$options{key}});
322 #
323 # $link->meta->make_mutable;
324 # $link->meta->add_method(
325 # '_source',
326 # sub { return $self }
327 # );
328 #
329 # return $link;
330 # },
331 #);
332 }
333  
334  
335 #-------------------------------------------------------------------------------
336  
337 sub _add_link_multiple {
338 0 ▶ my ($meta, $name, %options) = @_;
Subroutine _add_link_multiple: not called CC=1 SCAR=6.9
339
340 $meta->add_attribute(
341 $options{key},
342 0 isa => 'ArrayRef',
343 is => 'ro',
344 );
345
346 #$meta->add_method(
347 # $name,
348 # sub {
349 # my $self = shift;
350 # return $self->class->schema->link($options{class})->retrieve_list($options{args}, $self->{$options{key}});
351 # },
352 #);
353 }
354  
355  
356 #-------------------------------------------------------------------------------
357  
358 2 1;
« OOM/Object/Base.pm ↑ Summary OOM/Roles/Attribute.pm »