Lines 373-388
sub _numeric_column_type {
Link Here
|
373 |
|
373 |
|
374 |
=head3 to_api |
374 |
=head3 to_api |
375 |
|
375 |
|
376 |
my $object_for_api = $object->to_api; |
376 |
my $object_for_api = $object->to_api( |
|
|
377 |
{ |
378 |
[ embed => [ |
379 |
{ |
380 |
accessor => 'items', |
381 |
children => [ |
382 |
{ |
383 |
accessor => 'holds, |
384 |
children => [ |
385 |
... |
386 |
] |
387 |
} |
388 |
] |
389 |
}, |
390 |
... |
391 |
] ] |
392 |
} |
393 |
); |
377 |
|
394 |
|
378 |
Returns a representation of the object, suitable for API output. |
395 |
Returns a representation of the object, suitable for API output. |
379 |
|
396 |
|
380 |
=cut |
397 |
=cut |
381 |
|
398 |
|
382 |
sub to_api { |
399 |
sub to_api { |
383 |
my ( $self, $embeds ) = @_; |
400 |
my ( $self, $params ) = @_; |
384 |
my $json_object = $self->TO_JSON; |
401 |
my $json_object = $self->TO_JSON; |
385 |
|
402 |
|
|
|
403 |
my $embeds = $params->{embed}; |
404 |
|
386 |
# Rename attributes if there's a mapping |
405 |
# Rename attributes if there's a mapping |
387 |
if ( $self->can('to_api_mapping') ) { |
406 |
if ( $self->can('to_api_mapping') ) { |
388 |
foreach my $column ( keys %{ $self->to_api_mapping } ) { |
407 |
foreach my $column ( keys %{ $self->to_api_mapping } ) { |
Lines 404-420
sub to_api {
Link Here
|
404 |
|
423 |
|
405 |
if ($embeds) { |
424 |
if ($embeds) { |
406 |
foreach my $embed (@$embeds) { |
425 |
foreach my $embed (@$embeds) { |
407 |
my ( $curr, $next ) = split /\s*\.\s*/, $embed, 2; |
426 |
my $curr = $embed->{accessor}; |
408 |
my @nxembeds; |
427 |
my $next = $embed->{children}; |
409 |
|
428 |
|
410 |
@nxembeds = ($next) if $next; |
429 |
my @nxembeds; |
|
|
430 |
@nxembeds = @{ $next } if $next; |
411 |
|
431 |
|
412 |
my $children = $self->$curr; |
432 |
my $children = $self->$curr; |
413 |
if ( ref $children eq 'ARRAY' ) { |
433 |
if ( ref $children eq 'ARRAY' ) { |
414 |
my @list; |
434 |
my @list; |
415 |
my $pos = 0; |
435 |
my $pos = 0; |
416 |
foreach my $child (@$children) { |
436 |
foreach my $child (@$children) { |
417 |
my $res = $child->to_api( \@nxembeds ); |
437 |
my $res = $child->to_api({ embed => \@nxembeds }); |
418 |
$res = { $json_object->{$curr}->[$pos], $res } |
438 |
$res = { $json_object->{$curr}->[$pos], $res } |
419 |
if defined $json_object->{$curr} |
439 |
if defined $json_object->{$curr} |
420 |
&& defined $json_object->{$curr}->[$pos]; |
440 |
&& defined $json_object->{$curr}->[$pos]; |
Lines 424-430
sub to_api {
Link Here
|
424 |
$json_object->{$curr} = \@list; |
444 |
$json_object->{$curr} = \@list; |
425 |
} |
445 |
} |
426 |
else { |
446 |
else { |
427 |
my $res = $children->to_api( \@nxembeds ); |
447 |
my $res = $children->to_api({ embed => \@nxembeds }); |
428 |
$res = { $json_object->{$curr}, $res } |
448 |
$res = { $json_object->{$curr}, $res } |
429 |
if defined $json_object->{$curr}; |
449 |
if defined $json_object->{$curr}; |
430 |
$json_object->{$curr} = $res; |
450 |
$json_object->{$curr} = $res; |