| 1 | package HAL::Tiny; | ||||||||||
| 2 | 2 | ▶ | use 5.008001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use JSON qw/encode_json/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | our $VERSION = "0.01"; | |||||||||
| 9 | |||||||||||
| 10 | sub new { | ||||||||||
| 11 | 3 | ▶ | my ($class, %args) = @_; | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 12 | |||||||||||
| 13 | my ($state, $links, $embedded) | ||||||||||
| 14 | 3 | = @args{qw/state links embedded/}; | |||||||||
| 15 | |||||||||||
| 16 | 3 | return bless +{ | |||||||||
| 17 | state => $state, | ||||||||||
| 18 | links => $links, | ||||||||||
| 19 | embedded => $embedded, | ||||||||||
| 20 | }, $class; | ||||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | sub as_hash { | ||||||||||
| 24 | 6 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
as_hash: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||
| 25 | |||||||||||
| 26 | 6 | my %hash; | |||||||||
| 27 | |||||||||||
| 28 | 6 | ▶ | if (my $state = $self->{state}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 6 | %hash = %{ $self->{state} }; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 6 | ▶ | if (my $links = $self->{links}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 6 | my $v = +{}; | |||||||||
| 34 | 6 | for my $rel (keys %$links) { | |||||||||
| 35 | $v->{$rel} = +{ | ||||||||||
| 36 | 8 | href => $links->{$rel}, | |||||||||
| 37 | } | ||||||||||
| 38 | } | ||||||||||
| 39 | 6 | $hash{_links} = $v; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 6 | ▶ | if (my $embedded = $self->{embedded}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 2 | my $v = +{}; | |||||||||
| 44 | 2 | for my $rel (keys %$embedded) { | |||||||||
| 45 | 2 | ▶ | if (ref $embedded->{$rel} eq 'ARRAY') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 2 | my @hashed = map { $_->as_hash } @{$embedded->{$rel}}; | |||||||||
| 47 | 2 | $v->{$rel} = \@hashed; | |||||||||
| 48 | } else { | ||||||||||
| 49 | 0 | $v->{$rel} = $embedded->{$rel}->as_hash; | |||||||||
| 50 | } | ||||||||||
| 51 | } | ||||||||||
| 52 | 2 | $hash{_embedded} = $v; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 6 | return \%hash; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | sub as_json { | ||||||||||
| 59 | 2 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
as_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 60 | 2 | my $hash = $self->as_hash; | |||||||||
| 61 | 2 | return encode_json($hash); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | |||||||||||
| 65 | 2 | 1; | |||||||||
| 66 | __END__ | ||||||||||