| 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
| |||||||||||
| 206 | { | ||||||||||
| 207 | 0 | my @ids; | |||||||||
| 208 | 0 | ▶ | if(ref $args[0] eq 'ARRAY') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 223 | } | ||||||||||
| 224 | 0 | ▶ | return unless $self->{$field_name}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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 | $meta->add_method( | ||||||||||
| 254 | $name, | ||||||||||
| 255 | sub { | ||||||||||
| 256 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||
| 257 | 0 | ▶ | if(@_) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 258 | { | ||||||||||
| 259 | 0 | my $val = shift; | |||||||||
| 260 | 0 | ▶ | $self->$field_name($val ? _id($val) : undef); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 261 | 0 | ▶ | return unless defined wantarray; # avoid needless retrieval | ||||||||
|
Branch: 1 branch
| |||||||||||
| 262 | } | ||||||||||
| 263 | 0 | ▶ | return unless $self->{$options{key}}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 264 | 0 | return $self->class->schema->class($options{class})->retrieve($self->{$options{key}}); | |||||||||
| 265 | }, | ||||||||||
| 266 | 0 | ); | |||||||||
| 267 | } | ||||||||||
| 268 | |||||||||||
| 269 | |||||||||||
| 270 | #------------------------------------------------------------------------------- | ||||||||||
| 271 | |||||||||||
| 272 | sub has_link { | ||||||||||
| 273 | 0 | ▶ | my ($meta, $name, %options) = @_; | ||||||||
|
Subroutine
has_link: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 274 | |||||||||||
| 275 | $meta->link({ | ||||||||||
| 276 | 0 | %{$meta->link}, | |||||||||
| 277 | $name => \%options | ||||||||||
| 278 | }); | ||||||||||
| 279 | |||||||||||
| 280 | 0 | given ($options{type}) { | |||||||||
| 281 | 0 | when ('single') { | |||||||||
| 282 | 0 | goto &_add_link_single; | |||||||||
| 283 | } | ||||||||||
| 284 | 0 | when ('multiple') { | |||||||||
| 285 | 0 | goto &_add_link_multiple; | |||||||||
| 286 | } | ||||||||||
| 287 | } | ||||||||||
| 288 | } | ||||||||||
| 289 | |||||||||||
| 290 | |||||||||||
| 291 | #------------------------------------------------------------------------------- | ||||||||||
| 292 | |||||||||||
| 293 | sub _add_link_single { | ||||||||||
| 294 | 0 | ▶ | my ($meta, $name, %options) = @_; | ||||||||
|
Subroutine
_add_link_single: not called CC=1 SCAR=6.9
| |||||||||||
| 295 | |||||||||||
| 296 | $meta->add_attribute( | ||||||||||
| 297 | $options{key}, | ||||||||||
| 298 | 0 | isa => 'Int', | |||||||||
| 299 | is => 'ro', | ||||||||||
| 300 | ); | ||||||||||
| 301 | |||||||||||
| 302 | #$meta->add_method( | ||||||||||
| 303 | # $name, | ||||||||||
| 304 | # sub { | ||||||||||
| 305 | # my $self = shift; | ||||||||||
| 306 | # | ||||||||||
| 307 | # # FIXME - this is a bit broken, remembers between different objects! | ||||||||||
| 308 | # # Looks like the method is being added to the whole class, not the individual object | ||||||||||
| 309 | # # Maybe this code needs to go in Base.pm instead, in an "around create{...}" block | ||||||||||
| 310 | # # to add these methods in when a new object is created. | ||||||||||
| 311 | # state $link = $self->class->schema->link($options{class})->retrieve($options{args}, $self->{$options{key}}); | ||||||||||
| 312 | # | ||||||||||
| 313 | # $link->meta->make_mutable; | ||||||||||
| 314 | # $link->meta->add_method( | ||||||||||
| 315 | # '_source', | ||||||||||
| 316 | # sub { return $self } | ||||||||||
| 317 | # ); | ||||||||||
| 318 | # | ||||||||||
| 319 | # return $link; | ||||||||||
| 320 | # }, | ||||||||||
| 321 | #); | ||||||||||
| 322 | } | ||||||||||
| 323 | |||||||||||
| 324 | |||||||||||
| 325 | #------------------------------------------------------------------------------- | ||||||||||
| 326 | |||||||||||
| 327 | sub _add_link_multiple { | ||||||||||
| 328 | 0 | ▶ | my ($meta, $name, %options) = @_; | ||||||||
|
Subroutine
_add_link_multiple: not called CC=1 SCAR=6.9
| |||||||||||
| 329 | |||||||||||
| 330 | $meta->add_attribute( | ||||||||||
| 331 | $options{key}, | ||||||||||
| 332 | 0 | isa => 'ArrayRef', | |||||||||
| 333 | is => 'ro', | ||||||||||
| 334 | ); | ||||||||||
| 335 | |||||||||||
| 336 | #$meta->add_method( | ||||||||||
| 337 | # $name, | ||||||||||
| 338 | # sub { | ||||||||||
| 339 | # my $self = shift; | ||||||||||
| 340 | # return $self->class->schema->link($options{class})->retrieve_list($options{args}, $self->{$options{key}}); | ||||||||||
| 341 | # }, | ||||||||||
| 342 | #); | ||||||||||
| 343 | } | ||||||||||
| 344 | |||||||||||
| 345 | |||||||||||
| 346 | #------------------------------------------------------------------------------- | ||||||||||
| 347 | |||||||||||
| 348 | 2 | 1; | |||||||||