| Filename | /home/mickey/git_tree/PONAPI/Server/lib/PONAPI/Builder/Role/HasPagination.pm |
| Statements | Executed 10 statements in 529µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 520µs | 544µs | PONAPI::Builder::Role::HasPagination::BEGIN@7 |
| 1 | 1 | 1 | 17µs | 2.29ms | PONAPI::Builder::Role::HasPagination::BEGIN@4 |
| 1 | 1 | 1 | 7µs | 7µs | PONAPI::Builder::Role::HasPagination::BEGIN@6 |
| 1 | 1 | 1 | 7µs | 82µs | PONAPI::Builder::Role::HasPagination::BEGIN@81 |
| 0 | 0 | 0 | 0s | 0s | PONAPI::Builder::Role::HasPagination::_hash_to_uri_query |
| 0 | 0 | 0 | 0s | 0s | PONAPI::Builder::Role::HasPagination::add_pagination_links |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | # ABSTRACT: document builder - role - pagination | ||||
| 2 | package PONAPI::Builder::Role::HasPagination; | ||||
| 3 | |||||
| 4 | 2 | 47µs | 2 | 4.57ms | # spent 2.29ms (17µs+2.28) within PONAPI::Builder::Role::HasPagination::BEGIN@4 which was called:
# once (17µs+2.28ms) by Module::Runtime::require_module at line 4 # spent 2.29ms making 1 call to PONAPI::Builder::Role::HasPagination::BEGIN@4
# spent 2.28ms making 1 call to Moose::Role::import |
| 5 | |||||
| 6 | 2 | 22µs | 1 | 7µs | # spent 7µs within PONAPI::Builder::Role::HasPagination::BEGIN@6 which was called:
# once (7µs+0s) by Module::Runtime::require_module at line 6 # spent 7µs making 1 call to PONAPI::Builder::Role::HasPagination::BEGIN@6 |
| 7 | 2 | 430µs | 1 | 544µs | # spent 544µs (520+24) within PONAPI::Builder::Role::HasPagination::BEGIN@7 which was called:
# once (520µs+24µs) by Module::Runtime::require_module at line 7 # spent 544µs making 1 call to PONAPI::Builder::Role::HasPagination::BEGIN@7 |
| 8 | |||||
| 9 | # requires 'links_builder'; | ||||
| 10 | # requires 'req_path'; | ||||
| 11 | |||||
| 12 | # self isn't really part of pagination, but it can be overriden here | ||||
| 13 | 1 | 4µs | my %allowed_page_keys = map +($_=>1), qw/ | ||
| 14 | first | ||||
| 15 | last | ||||
| 16 | next | ||||
| 17 | prev | ||||
| 18 | self | ||||
| 19 | /; | ||||
| 20 | |||||
| 21 | sub add_pagination_links { | ||||
| 22 | my ($self, %page_links) = @_; | ||||
| 23 | |||||
| 24 | $page_links{self} ||= delete $page_links{current} | ||||
| 25 | if exists $page_links{current}; | ||||
| 26 | |||||
| 27 | foreach my $link_name ( keys %page_links ) { | ||||
| 28 | die "Tried to add pagination link `$link_name`, not allowed by the spec" | ||||
| 29 | unless exists $allowed_page_keys{ $link_name }; | ||||
| 30 | } | ||||
| 31 | |||||
| 32 | my $link = $self->req_path; | ||||
| 33 | |||||
| 34 | my $uri = URI->new($link); | ||||
| 35 | my $path = $uri->path; | ||||
| 36 | |||||
| 37 | $self->links_builder->add_links( | ||||
| 38 | map { | ||||
| 39 | my $query = $self->_hash_to_uri_query( { | ||||
| 40 | page => $page_links{$_} | ||||
| 41 | }, $uri ); | ||||
| 42 | ( $_ => $path . '?' . $query ) | ||||
| 43 | } | ||||
| 44 | grep scalar keys %{ $page_links{$_} || {} }, | ||||
| 45 | keys %page_links | ||||
| 46 | ); | ||||
| 47 | } | ||||
| 48 | |||||
| 49 | sub _hash_to_uri_query { | ||||
| 50 | my ($self, $data, $u) = @_; | ||||
| 51 | $u ||= URI->new("", "http"); | ||||
| 52 | |||||
| 53 | for my $d_k ( sort keys %$data ) { | ||||
| 54 | my $d_v = $data->{$d_k}; | ||||
| 55 | defined($d_v) or next; | ||||
| 56 | |||||
| 57 | if ( ref $d_v ne 'HASH' ) { | ||||
| 58 | $u->query_param( $d_k => | ||||
| 59 | join ',' => ( ref $d_v eq 'ARRAY' ? @{$d_v} : $d_v ) ); | ||||
| 60 | next; | ||||
| 61 | } | ||||
| 62 | |||||
| 63 | # HASH | ||||
| 64 | for my $k ( sort keys %{$d_v} ) { | ||||
| 65 | my $v = $d_v->{$k}; | ||||
| 66 | defined($v) or next; | ||||
| 67 | |||||
| 68 | die "_hash_to_uri_query: nested value can be scalar/arrayref only" | ||||
| 69 | unless !ref $v or ref $v eq 'ARRAY'; | ||||
| 70 | |||||
| 71 | $u->query_param( $d_k . '[' . $k . ']' => | ||||
| 72 | join ',' => ( ref $v eq 'ARRAY' ? @{$v} : $v ) ); | ||||
| 73 | } | ||||
| 74 | } | ||||
| 75 | |||||
| 76 | return $u->query; | ||||
| 77 | } | ||||
| 78 | |||||
| - - | |||||
| 81 | 3 | 26µs | 2 | 158µs | # spent 82µs (7+76) within PONAPI::Builder::Role::HasPagination::BEGIN@81 which was called:
# once (7µs+76µs) by Module::Runtime::require_module at line 81 # spent 82µs making 1 call to PONAPI::Builder::Role::HasPagination::BEGIN@81
# spent 76µs making 1 call to Moose::Role::unimport |
| 82 | |||||
| 83 | __END__ |