DBIC/Resource/HAL/Role/DBIC.pm

filter: Statementstmt 97.2% 71 / 73 Branchbran 79.4% 27 / 34 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 87.9% 117 / 133 CC 24 SCAR 18.1
CC
24
Cov
91%
_hal_page_links
9.2
render_item_as_hal_hash
7.3
_render_prefetch
6.1
CRAP 6.1
« DBIC/Resource/HAL/Role/SetWritable.pm ↑ Summary DBIC/Resource/ActiveModel/Role/DBIC.pm »
1 package WebAPI::DBIC::Resource::HAL::Role::DBIC;
2 25 $WebAPI::DBIC::Resource::HAL::Role::DBIC::VERSION = '0.004002';
3  
4 25 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use Devel::Dwarn;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 25 ▶use JSON::MaybeXS qw(JSON);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 25 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 25 requires 'get_url_for_item_relationship';
12 25 requires 'render_item_as_plain_hash';
13 25 requires 'path_for_item';
14 25 requires 'add_params_to_url';
15 25 requires 'prefetch';
16  
17  
18 103 ▶sub render_item_as_hal_hash {
Subroutine render_item_as_hal_hash: called CC=7 SCAR=19.9
Pod undocumented
19 103 my ($self, $item) = @_;
20  
21 103 my $data = $self->render_item_as_plain_hash($item);
22  
23 103 my $itemurl = $self->path_for_item($item);
24 $data->{_links}{self} = {
25 103 href => $self->add_params_to_url($itemurl, {}, {})->as_string,
26 };
27  
28 103 ▶ $self->_render_prefetch($item, $data, $_) for @{$self->prefetch||[]};
Condition50%1/2
$self->prefetch || []
l!l
1030
MC/DC0%0/1
$self->prefetch || []
$self->prefetch
Truth table50%1/2
A
A= $self->prefetch
Aresult
11
00
29  
30 103 my $curie = (0) ? "r" : ""; # XXX we don't use CURIE syntax yet
31  
32 # add links for relationships
33 103 for my $relname ($item->result_class->relationships) {
34  
35 436 ▶ my $url = $self->get_url_for_item_relationship($item, $relname)
Branch: 1 branch
BranchTrueFalseTotal
my $url = $self->get_url_for_item_relationship($item, $relname) or next 301 135 436
36 or next;
37  
38 301 ▶ $data->{_links}{ ($curie?"$curie:":"") . $relname} = { href => $url->as_string };
Branch: 1 branch
BranchTrueFalseTotal
$curie ? : 0 301 301
39 }
40 103 ▶ if ($curie) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curie) 0 103 103
41 $data->{_links}{curies} = [{
42 0 name => $curie,
43 href => "http://docs.acme.com/relations/{rel}", # XXX
44 templated => JSON->true,
45 }];
46 }
47  
48 103 return $data;
49 }
50  
51  
52 63 ▶sub _render_prefetch {
Subroutine _render_prefetch: called CC=6 SCAR=18.1
53 63 my ($self, $item, $data, $prefetch) = @_;
54  
55 63 ▶ while (my ($rel, $sub_rel) = each %{$prefetch}){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$prefetch;}) 55 63 118
56 55 ▶ next if $rel eq 'self';
Branch: 1 branch
BranchTrueFalseTotal
if $rel eq "self" 6 49 55
57  
58 49 my $subitem = $item->$rel();
59  
60 # XXX perhaps render_item_as_hal_hash but requires cloned WM, eg without prefetch
61 # If we ever do render_item_as_hal_hash then we need to ensure that "a link
62 # inside an embedded resource implicitly relates to that embedded
63 # resource and not the parent."
64 # See http://blog.stateless.co/post/13296666138/json-linking-with-hal
65 49 ▶ if (not defined $subitem) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $subitem) { } 0 49 49
elsif ($subitem->isa('DBIx::Class::ResultSet')) { } 8 41 49
66 0 $data->{_embedded}{$rel} = undef; # show an explicit null from a prefetch
67 }
68 elsif ($subitem->isa('DBIx::Class::ResultSet')) { # one-to-many rel
69 8 ▶ my $rel_set_resource = $self->web_machine_resource(
Branch: 1 branch
BranchTrueFalseTotal
ref $sub_rel eq 'ARRAY' ? : 8 0 8
70 set => $subitem,
71 prefetch => ref $sub_rel eq 'ARRAY' ? $sub_rel : [$sub_rel],
72 );
73 8 $data->{_embedded}{$rel} = $rel_set_resource->render_set_as_list_of_hal($subitem);
74 }
75 else {
76 41 $data->{_embedded}{$rel} = $self->render_item_as_plain_hash($subitem);
77 }
78 }
79 }
80  
81  
82 32 ▶sub render_set_as_list_of_hal {
Subroutine render_set_as_list_of_hal: called CC=2 SCAR=6.9
Pod undocumented
83 32 my ($self, $set, $render_method) = @_;
84 32 ▶ $render_method ||= 'render_item_as_hal_hash';
Condition100%2/2
$render_method ||= "render_item_as_hal_hash"
l!l
248
MC/DC100%1/1
$render_method ||= "render_item_as_hal_hash"
$render_method
Truth table100%2/2
A
A= $render_method
Aresult
11
00
85  
86 32 my $set_data = [ map { $self->$render_method($_) } $set->all ];
87  
88 32 return $set_data;
89 }
90  
91  
92 24 ▶sub render_set_as_hal {
Subroutine render_set_as_hal: called CC=4 SCAR=13.9
Pod undocumented
93 24 my ($self, $set) = @_;
94  
95 # some params, like distinct, mean we're not returning full resource representations(?)
96 # so render the contents of the _embedded set as plain JSON
97 24 ▶ my $render_method = ($self->param('distinct'))
Branch: 1 branch
BranchTrueFalseTotal
$self->param('distinct') ? : 1 23 24
98 ? 'render_item_as_plain_hash'
99 : 'render_item_as_hal_hash';
100 24 my $set_data = $self->render_set_as_list_of_hal($set, $render_method);
101  
102 24 my $data = {};
103  
104 24 my $total_items;
105 24 ▶ if (($self->param('with')||'') =~ /count/) { # XXX
Branch: 1 branch
BranchTrueFalseTotal
if (($self->param("with") || "") =~ /count/) 1 23 24
Condition100%2/2
$self->param('with') || ''
l!l
123
MC/DC100%1/1
$self->param('with') || ''
$self->param('with')
Truth table100%2/2
A
A= $self->param('with')
Aresult
11
00
106 1 $total_items = $set->pager->total_entries;
107 1 $data->{_meta}{count} = $total_items;
108 }
109  
110 24 my ($prefix, $rel) = $self->uri_for(result_class => $set->result_class);
111 $data->{_embedded} = {
112 24 $rel => $set_data,
113 };
114 $data->{_links} = {
115 24 $self->_hal_page_links($set, "$prefix/$rel", scalar @$set_data, $total_items),
116 };
117  
118 24 return $data;
119 }
120  
121  
122 24 ▶sub _hal_page_links {
Subroutine _hal_page_links: called CC=9 SCAR=22.2
123 24 my ($self, $set, $base, $page_items, $total_items) = @_;
124  
125 # XXX we ought to allow at least the self link when not pages
126 24 ▶ return () unless $set->is_paged;
Branch: 1 branch
BranchTrueFalseTotal
unless $set->is_paged 0 24 24
127  
128 # XXX we break encapsulation here, sadly, because calling
129 # $set->pager->current_page triggers a "select count(*)".
130 # XXX When we're using a later version of DBIx::Class we can use this:
131 # https://metacpan.org/source/RIBASUSHI/DBIx-Class-0.08208/lib/DBIx/Class/ResultSet/Pager.pm
132 # and do something like $rs->pager->total_entries(sub { 99999999 })
133 24 ▶ my $rows = $set->{attrs}{rows} or confess "panic: rows not set";
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $set->{"attrs"}{"rows"} or confess("panic: rows not set") 24 0 24
134 24 ▶ my $page = $set->{attrs}{page} or confess "panic: page not set";
Branch: 1 branch
BranchTrueFalseTotal
my $page = $set->{"attrs"}{"page"} or confess("panic: page not set") 24 0 24
135  
136 # XXX this self link this should probably be subtractive, ie include all
137 # params by default except any known to cause problems
138 24 my $url = $self->add_params_to_url($base, { distinct=>1, with=>1, me=>1 }, { rows => $rows });
139 24 my $linkurl = $url->as_string;
140 24 $linkurl .= "&page="; # hack to optimize appending page 5 times below
141  
142 24 my @link_kvs;
143 24 push @link_kvs, self => {
144 href => $linkurl.($page),
145 title => $set->result_class,
146 };
147 24 ▶ push @link_kvs, next => { href => $linkurl.($page+1) }
Branch: 1 branch
BranchTrueFalseTotal
if $page_items == $rows 6 18 24
148 if $page_items == $rows;
149 24 ▶ push @link_kvs, prev => { href => $linkurl.($page-1) }
Branch: 1 branch
BranchTrueFalseTotal
if $page > 1 1 23 24
150 if $page > 1;
151 24 ▶ push @link_kvs, first => { href => $linkurl.1 }
Branch: 1 branch
BranchTrueFalseTotal
if $page > 1 1 23 24
152 if $page > 1;
153 24 ▶ push @link_kvs, last => { href => $linkurl.$set->pager->last_page }
Branch: 1 branch
BranchTrueFalseTotal
if $total_items and $page != $set->pager->last_page 1 23 24
Condition66%2/3
$total_items and $page != $set->pager->last_page
!ll&&!rl&&r
2301
MC/DC50%1/2
$total_items and $page != $set->pager->last_page
$total_items $page != $set->pager->last_page
Truth table66%2/3
A and B
A= $total_itemsB= $page != $set->pager->last_page
ABresult
0X0
100
111
154 if $total_items and $page != $set->pager->last_page;
155  
156 24 return @link_kvs;
157 }
158  
159  
160 25 1;
161  
162 __END__
« DBIC/Resource/HAL/Role/SetWritable.pm ↑ Summary DBIC/Resource/ActiveModel/Role/DBIC.pm »