Mojito/Collection/Present.pm

filter: Statementstmt 44.7% 34 / 76 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 26.3% 5 / 19 Podpod n/a totaltotal 34.5% 39 / 113 CC 10 SCAR 18.9
CC
10
Cov
36%
_build_next_page_route
20.0
_build_previous_page_route
12.0
_build_collection
6.0
CRAP 6.6
« Mojito/Role/DB/OID.pm ↑ Summary Mojito/Role/Config.pm »
1 4 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Mojito::Collection::Present;
3 4 $Mojito::Collection::Present::VERSION = '0.25';
4 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Mojito::Collection::CRUD;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use List::MoreUtils qw/ first_index /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 has db => (is => 'ro', lazy => 1);
10  
11 4 has config => (is => 'ro', lazy => 1);
12  
13 4 has 'collection' => (
14 is => 'ro',
15 isa => HashRef,
16 lazy => 1,
17 builder => '_build_collection',
18 );
19 0 ▶sub _build_collection {
Subroutine _build_collection: not called CC=2 SCAR=17.9
20 0 my $self = shift;
21 0 ▶ die "Must have collection id" if !$self->collection_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->collection_id 0 0 0
22 0 return Mojito::Collection::CRUD->new(config => $self->config, db => $self->db)->read($self->collection_id);
23 }
24  
25 4 has 'collection_id' => (
26 is => 'ro',
27 isa => Value,
28 required => 1,
29 );
30  
31 4 has 'page_ids' => (
32 is => 'ro',
33 isa => ArrayRef,
34 lazy => 1,
35 builder => '_build_page_ids',
36 );
37 0 ▶sub _build_page_ids {
Subroutine _build_page_ids: not called CC=1 SCAR=6.9
38 0 return $_[0]->collection->{'collected_page_ids'};
39 }
40  
41 # The focus_page is the slot number of the page being view where the slot number
42 # comes from the ArrayRef of pages that make up the collection.
43 # The index page (list of links to all pages of the collection) is, by convention,
44 # indicated by a -1.
45 4 has 'focus_page_id' => (
46 is => 'ro',
47 );
48  
49 4 has 'focus_page_number' => (
50 is => 'rw',
51 isa => Int,
52 builder => '_build_focus_page_number',
53 );
54 0 ▶sub _build_focus_page_number {
Subroutine _build_focus_page_number: not called CC=2 SCAR=17.9
55 0 my $self = shift;
56
57 0 ▶ return -1 if !$self->focus_page_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->focus_page_id 0 0 0
58 0 ▶ return first_index { $_ eq $self->focus_page_id } @{$self->page_ids};
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 }
60  
61 4 has 'focus_page_route' => (
62 is => 'ro',
63 builder => '_build_focus_page_route',
64 );
65 0 ▶sub _build_focus_page_route {
Subroutine _build_focus_page_route: not called CC=2 SCAR=17.9
66 0 my $self = shift;
67
68 0 my $URI_fragment;
69 # Handle index case
70 0 ▶ if ($self->focus_page_number == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->focus_page_number == -1) { } 0 0 0
71 0 $URI_fragment = 'collection/' . $self->collection->{id};
72 }
73 else {
74 0 $URI_fragment = 'page/' . $self->page_ids->[$self->focus_page_number];
75 }
76 0 return $URI_fragment;
77 }
78  
79 4 has 'index_page_route' => (
80 is => 'ro',
81 lazy => 1,
82 builder => '_build_index_page_route',
83 );
84 0 ▶sub _build_index_page_route {
Subroutine _build_index_page_route: not called CC=1 SCAR=6.9
85 0 return 'collection/' . $_[0]->index_page_id;
86 }
87  
88 4 has 'next_page_route' => (
89 is => 'ro',
90 builder => '_build_next_page_route',
91 );
92 0 ▶sub _build_next_page_route {
Subroutine _build_next_page_route: not called CC=4 SCAR=30.0
93 0 my $self = shift;
94
95 # Are we focused on index => first_page
96 # Are we focused on last => index
97 # Are we focused on any other page => focus + 1
98 0 my $focus = $self->focus_page_number;
99 0 ▶ warn "index page id empty: " if !$self->index_page_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->index_page_id 0 0 0
100 0 my $collection_route = 'collection/' . $self->index_page_id;
101 0 my $next;
102 0 ▶ if ($focus == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($focus == -1) { } 0 0 0
elsif ($focus == $self->last_page_number) { } 0 0 0
103 0 $next = $collection_route . '/page/' . $self->first_page_id;
104 }
105 elsif ($focus == $self->last_page_number) {
106 0 $next = $collection_route;
107 }
108 else {
109 0 $next = $collection_route . '/page/' . $self->page_ids->[($self->focus_page_number + 1)];
110 }
111
112 0 return $next;
113 }
114  
115 4 has 'previous_page_route' => (
116 is => 'ro',
117 builder => '_build_previous_page_route',
118 );
119 0 ▶sub _build_previous_page_route {
Subroutine _build_previous_page_route: not called CC=3 SCAR=24.8
120 0 my $self = shift;
121
122 # Are we focused on index => last page
123 # Are we focused on first => index
124 # Are we focused on any other page => focus - 1
125 0 my $focus = $self->focus_page_number;
126 0 my $collection_route = 'collection/' . $self->index_page_id;
127 0 my $previous;
128 0 ▶ if ($focus == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($focus == -1) { } 0 0 0
elsif ($focus == $self->first_page_number) { } 0 0 0
129 0 $previous = $collection_route . '/page/' . $self->last_page_id;
130 }
131 elsif ($focus == $self->first_page_number) {
132 0 $previous = 'collection/' . $self->index_page_id;
133 }
134 else {
135 0 $previous = $collection_route . '/page/' . $self->page_ids->[($self->focus_page_number - 1)];
136 }
137
138 0 return $previous;
139 }
140  
141 4 has 'first_page_id' => (
142 is => 'ro',
143 lazy => 1,
144 builder => '_build_first_page_id',
145 );
146 0 ▶sub _build_first_page_id {
Subroutine _build_first_page_id: not called CC=1 SCAR=6.9
147 0 $_[0]->page_ids->[0];
148 }
149  
150 4 has 'last_page_id' => (
151 is => 'ro',
152 lazy => 1,
153 builder => '_build_last_page_id',
154 );
155 0 ▶sub _build_last_page_id {
Subroutine _build_last_page_id: not called CC=1 SCAR=6.9
156 0 $_[0]->page_ids->[-1];
157 }
158  
159 4 has 'index_page_id' => (
160 is => 'ro',
161 lazy => 1,
162 builder => '_build_index_page_id',
163 );
164 0 ▶sub _build_index_page_id { $_[0]->collection->{_id}||$_[0]->collection->{id} }
Subroutine _build_index_page_id: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->collection->{"_id"} or $_[0]->collection->{"id"} 0 0 0
165  
166 4 has 'index_page_number' => (
167 is => 'ro',
168 lazy => 1,
169 builder => '_build_index_page_number',
170 );
171 0 ▶sub _build_index_page_number { -1 }
Subroutine _build_index_page_number: not called CC=1 SCAR=6.9
172  
173 4 has 'first_page_number' => (
174 is => 'ro',
175 lazy => 1,
176 builder => '_build_first_page_number',
177 );
178 0 ▶sub _build_first_page_number { 0 }
Subroutine _build_first_page_number: not called CC=1 SCAR=6.9
179  
180  
181 4 has 'last_page_number' => (
182 is => 'ro',
183 lazy => 1,
184 builder => '_build_last_page_number',
185 );
186 0 ▶sub _build_last_page_number {
Subroutine _build_last_page_number: not called CC=1 SCAR=6.9
187 0 (scalar @{$_[0]->page_ids}) - 1;
188 }
189  
190 4 1
« Mojito/Role/DB/OID.pm ↑ Summary Mojito/Role/Config.pm »