|
Lines 372-399
sub to_api {
Link Here
|
| 372 |
my ( $self ) = @_; |
372 |
my ( $self ) = @_; |
| 373 |
my $json_object = $self->TO_JSON; |
373 |
my $json_object = $self->TO_JSON; |
| 374 |
|
374 |
|
|
|
375 |
my $to_api_mapping = $self->to_api_mapping; |
| 376 |
|
| 375 |
# Rename attributes if there's a mapping |
377 |
# Rename attributes if there's a mapping |
| 376 |
if ( $self->can('to_api_mapping') ) { |
378 |
foreach my $column ( keys %{$to_api_mapping} ) { |
| 377 |
foreach my $column ( keys %{$self->to_api_mapping} ) { |
379 |
my $mapped_column = $to_api_mapping->{$column}; |
| 378 |
my $mapped_column = $self->to_api_mapping->{$column}; |
380 |
if ( exists $json_object->{$column} |
| 379 |
if ( exists $json_object->{$column} |
381 |
&& defined $mapped_column ) |
| 380 |
&& defined $mapped_column ) |
382 |
{ |
| 381 |
{ |
383 |
# key != undef |
| 382 |
# key != undef |
384 |
$json_object->{$mapped_column} = delete $json_object->{$column}; |
| 383 |
$json_object->{$mapped_column} = delete $json_object->{$column}; |
385 |
} |
| 384 |
} |
386 |
elsif ( exists $json_object->{$column} |
| 385 |
elsif ( exists $json_object->{$column} |
387 |
&& !defined $mapped_column ) |
| 386 |
&& !defined $mapped_column ) |
388 |
{ |
| 387 |
{ |
389 |
# key == undef |
| 388 |
# key == undef |
390 |
delete $json_object->{$column}; |
| 389 |
delete $json_object->{$column}; |
|
|
| 390 |
} |
| 391 |
} |
391 |
} |
| 392 |
} |
392 |
} |
| 393 |
|
393 |
|
| 394 |
return $json_object; |
394 |
return $json_object; |
| 395 |
} |
395 |
} |
| 396 |
|
396 |
|
|
|
397 |
=head3 to_api_mapping |
| 398 |
|
| 399 |
my $mapping = $object->to_api_mapping; |
| 400 |
|
| 401 |
Generic method that returns the attribute name mappings required to |
| 402 |
render the object on the API. |
| 403 |
|
| 404 |
Note: this only returns an empty I<hashref>. Each class should have its |
| 405 |
own mapping returned. |
| 406 |
|
| 407 |
=cut |
| 408 |
|
| 409 |
sub to_api_mapping { |
| 410 |
return {}; |
| 411 |
} |
| 412 |
|
| 413 |
=head3 from_api_mapping |
| 414 |
|
| 415 |
my $mapping = $object->from_api_mapping; |
| 416 |
|
| 417 |
Generic method that returns the attribute name mappings so the data that |
| 418 |
comes from the API is correctly renamed to match what is required for the DB. |
| 419 |
|
| 420 |
=cut |
| 421 |
|
| 422 |
sub from_api_mapping { |
| 423 |
my ( $self ) = @_; |
| 424 |
|
| 425 |
my $to_api_mapping = $self->to_api_mapping; |
| 426 |
|
| 427 |
unless ( $self->{_from_api_mapping} ) { |
| 428 |
while (my ($key, $value) = each %{ $to_api_mapping } ) { |
| 429 |
$self->{_from_api_mapping}->{$value} = $key |
| 430 |
if defined $value; |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
return $self->{_from_api_mapping}; |
| 435 |
} |
| 436 |
|
| 397 |
=head3 $object->unblessed_all_relateds |
437 |
=head3 $object->unblessed_all_relateds |
| 398 |
|
438 |
|
| 399 |
my $everything_into_one_hashref = $object->unblessed_all_relateds |
439 |
my $everything_into_one_hashref = $object->unblessed_all_relateds |
|
Lines 506-511
sub AUTOLOAD {
Link Here
|
| 506 |
return $r; |
546 |
return $r; |
| 507 |
} |
547 |
} |
| 508 |
|
548 |
|
|
|
549 |
=head3 attributes_from_api |
| 550 |
|
| 551 |
my $attributes = attributes_from_api( $params ); |
| 552 |
|
| 553 |
Returns the passed params, converted from API naming into the model. |
| 554 |
|
| 555 |
=cut |
| 556 |
|
| 557 |
sub attributes_from_api { |
| 558 |
my ( $self, $attributes ) = @_; |
| 559 |
|
| 560 |
my $mapping = $self->from_api_mapping; |
| 561 |
|
| 562 |
foreach my $attribute ( keys %{$mapping} ) { |
| 563 |
my $mapped_attribute = $mapping->{$attribute}; |
| 564 |
if ( exists $attributes->{$attribute} |
| 565 |
&& defined $mapped_attribute ) |
| 566 |
{ |
| 567 |
# key => !undef |
| 568 |
$attributes->{$mapped_attribute} = delete $attributes->{$attribute}; |
| 569 |
} |
| 570 |
elsif ( exists $attributes->{$attribute} |
| 571 |
&& !defined $mapped_attribute ) |
| 572 |
{ |
| 573 |
# key => undef / to be deleted |
| 574 |
delete $attributes->{$attribute}; |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
return $attributes; |
| 579 |
} |
| 580 |
|
| 509 |
=head3 _type |
581 |
=head3 _type |
| 510 |
|
582 |
|
| 511 |
This method must be defined in the child class. The value is the name of the DBIC resultset. |
583 |
This method must be defined in the child class. The value is the name of the DBIC resultset. |
| 512 |
- |
|
|