| | 36 | } |
| | 37 | |
| | 38 | # see docs below |
| | 39 | |
| | 40 | sub has_a { |
| | 41 | my $class = shift; |
| | 42 | my %args = @_; |
| | 43 | |
| | 44 | # Iterate over each remote object |
| | 45 | foreach my $parentclass (keys %args) { |
| | 46 | my $config = $args{$parentclass}; |
| | 47 | next unless $config; |
| | 48 | |
| | 49 | # Parameters |
| | 50 | my $column = $config->{column}; |
| | 51 | my $method = $config->{method}; |
| | 52 | my $parent_method = $config->{method}; |
| | 53 | |
| | 54 | # column is required |
| | 55 | if (!defined($column)) { |
| | 56 | die "Please specify a valid column for $parentclass" |
| | 57 | } |
| | 58 | |
| | 59 | # create a method name based on the column |
| | 60 | if (! defined $method) { |
| | 61 | $method = $column; |
| | 62 | $method =~ s/_id$//; |
| | 63 | } |
| | 64 | |
| | 65 | # die if we can't find a way to make a valid method |
| | 66 | # TBD check current list of columns to avoid clash |
| | 67 | if (! defined $method || ($method eq $column)) { |
| | 68 | die "Please define a valid method for $class->$column"; |
| | 69 | } |
| | 70 | |
| | 71 | print STDERR "adding method $method linking $column to $parentclass->lookup()\n"; |
| | 72 | no strict 'refs'; |
| | 73 | *{"${class}::$method"} = sub { |
| | 74 | my $obj = shift; |
| | 75 | # TBD - Add in-memory caching logic here with weak ref? |
| | 76 | return $parentclass->lookup($obj->column($column)); |
| | 77 | }; |
| | 78 | |
| | 79 | # now add to the parent |
| | 80 | if (!defined $parent_method) { |
| | 81 | $parent_method = lc($class); |
| | 82 | $parent_method =~ s/^.*:://; |
| | 83 | $parent_method .= 's'; # plural :) |
| | 84 | } |
| | 85 | print STDERR "adding method ${parentclass}::$parent_method\n"; |
| | 86 | *{"${parentclass}::$parent_method"} = sub { |
| | 87 | my $obj = shift; |
| | 88 | my $terms = shift; |
| | 89 | my $args = shift; |
| | 90 | # TBD - allow user defined extra terms here?... |
| | 91 | # TBD - use primary_key_to_terms |
| | 92 | return $class->search({$column => $obj->id}, $args); |
| | 93 | }; |
| | 94 | } # end of loop over class names |
| | 95 | return; |
| | 400 | |
| | 401 | Returns the list of properties. |
| | 402 | |
| | 403 | =head2 Class->has_a(ParentClass => { ... }, ParentClass2 => { ...} ) |
| | 404 | |
| | 405 | Creates utility methods that map this object to parent Data::ObjectDriver objects. |
| | 406 | |
| | 407 | Pass in a list of parent classes to map with a hash of parameters. The following parameters |
| | 408 | are recognized: |
| | 409 | |
| | 410 | =over 4 |
| | 411 | |
| | 412 | =item * column |
| | 413 | |
| | 414 | Name of the column(s) in this class to map with. Pass in a single string if |
| | 415 | the column is a singular key, an array ref if this is a composite key. |
| | 416 | |
| | 417 | =item * method [OPTIONAL] |
| | 418 | |
| | 419 | Name of the method to create in this class. Defaults to the column name without |
| | 420 | the _id suffix |
| | 421 | |
| | 422 | =item * parent_method [OPTIONAL] |
| | 423 | |
| | 424 | Name of the method created in the parent class. Default is the lowercased |
| | 425 | name of the current class with an 's' appended. |
| | 426 | |
| | 427 | =item * cached [OPTIONAL] |
| | 428 | |
| | 429 | If set to 1 then we will cache in-memory the resulting object inside this class. |
| | 430 | |
| | 431 | =back |