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