Lines 384-411
sub to_api {
Link Here
|
384 |
my ( $self ) = @_; |
384 |
my ( $self ) = @_; |
385 |
my $json_object = $self->TO_JSON; |
385 |
my $json_object = $self->TO_JSON; |
386 |
|
386 |
|
|
|
387 |
my $to_api_mapping = $self->to_api_mapping; |
388 |
|
387 |
# Rename attributes if there's a mapping |
389 |
# Rename attributes if there's a mapping |
388 |
if ( $self->can('to_api_mapping') ) { |
390 |
foreach my $column ( keys %{$to_api_mapping} ) { |
389 |
foreach my $column ( keys %{$self->to_api_mapping} ) { |
391 |
my $mapped_column = $to_api_mapping->{$column}; |
390 |
my $mapped_column = $self->to_api_mapping->{$column}; |
392 |
if ( exists $json_object->{$column} |
391 |
if ( exists $json_object->{$column} |
393 |
&& defined $mapped_column ) |
392 |
&& defined $mapped_column ) |
394 |
{ |
393 |
{ |
395 |
# key != undef |
394 |
# key != undef |
396 |
$json_object->{$mapped_column} = delete $json_object->{$column}; |
395 |
$json_object->{$mapped_column} = delete $json_object->{$column}; |
397 |
} |
396 |
} |
398 |
elsif ( exists $json_object->{$column} |
397 |
elsif ( exists $json_object->{$column} |
399 |
&& !defined $mapped_column ) |
398 |
&& !defined $mapped_column ) |
400 |
{ |
399 |
{ |
401 |
# key == undef |
400 |
# key == undef |
402 |
delete $json_object->{$column}; |
401 |
delete $json_object->{$column}; |
|
|
402 |
} |
403 |
} |
403 |
} |
404 |
} |
404 |
} |
405 |
|
405 |
|
406 |
return $json_object; |
406 |
return $json_object; |
407 |
} |
407 |
} |
408 |
|
408 |
|
|
|
409 |
=head3 to_api_mapping |
410 |
|
411 |
my $mapping = $object->to_api_mapping; |
412 |
|
413 |
Generic method that returns the attribute name mappings required to |
414 |
render the object on the API. |
415 |
|
416 |
Note: this only returns an empty I<hashref>. Each class should have its |
417 |
own mapping returned. |
418 |
|
419 |
=cut |
420 |
|
421 |
sub to_api_mapping { |
422 |
return {}; |
423 |
} |
424 |
|
425 |
=head3 from_api_mapping |
426 |
|
427 |
my $mapping = $object->from_api_mapping; |
428 |
|
429 |
Generic method that returns the attribute name mappings so the data that |
430 |
comes from the API is correctly renamed to match what is required for the DB. |
431 |
|
432 |
=cut |
433 |
|
434 |
sub from_api_mapping { |
435 |
my ( $self ) = @_; |
436 |
|
437 |
my $to_api_mapping = $self->to_api_mapping; |
438 |
|
439 |
unless ( $self->{_from_api_mapping} ) { |
440 |
while (my ($key, $value) = each %{ $to_api_mapping } ) { |
441 |
$self->{_from_api_mapping}->{$value} = $key |
442 |
if defined $value; |
443 |
} |
444 |
} |
445 |
|
446 |
return $self->{_from_api_mapping}; |
447 |
} |
448 |
|
409 |
=head3 $object->unblessed_all_relateds |
449 |
=head3 $object->unblessed_all_relateds |
410 |
|
450 |
|
411 |
my $everything_into_one_hashref = $object->unblessed_all_relateds |
451 |
my $everything_into_one_hashref = $object->unblessed_all_relateds |
Lines 518-523
sub AUTOLOAD {
Link Here
|
518 |
return $r; |
558 |
return $r; |
519 |
} |
559 |
} |
520 |
|
560 |
|
|
|
561 |
=head3 attributes_from_api |
562 |
|
563 |
my $attributes = attributes_from_api( $params ); |
564 |
|
565 |
Returns the passed params, converted from API naming into the model. |
566 |
|
567 |
=cut |
568 |
|
569 |
sub attributes_from_api { |
570 |
my ( $self, $attributes ) = @_; |
571 |
|
572 |
my $mapping = $self->from_api_mapping; |
573 |
|
574 |
foreach my $attribute ( keys %{$mapping} ) { |
575 |
my $mapped_attribute = $mapping->{$attribute}; |
576 |
if ( exists $attributes->{$attribute} |
577 |
&& defined $mapped_attribute ) |
578 |
{ |
579 |
# key => !undef |
580 |
$attributes->{$mapped_attribute} = delete $attributes->{$attribute}; |
581 |
} |
582 |
elsif ( exists $attributes->{$attribute} |
583 |
&& !defined $mapped_attribute ) |
584 |
{ |
585 |
# key => undef / to be deleted |
586 |
delete $attributes->{$attribute}; |
587 |
} |
588 |
} |
589 |
|
590 |
return $attributes; |
591 |
} |
592 |
|
521 |
=head3 _type |
593 |
=head3 _type |
522 |
|
594 |
|
523 |
This method must be defined in the child class. The value is the name of the DBIC resultset. |
595 |
This method must be defined in the child class. The value is the name of the DBIC resultset. |
524 |
- |
|
|