PageHistory/PageSet.pm

filter: Statementstmt 98.2% 57 / 58 Branchbran 81.8% 18 / 22 Conditioncond 66.6% 10 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 86.1% 106 / 123 CC 19 SCAR 13.8
CC
19
Cov
89%
add
7.2
_coerce_pages
5.2
page_index
5.0
CRAP 4.0
↑ Summary PageHistory.pm »
1 package Dancer::Plugin::PageHistory::PageSet;
2  
3 =head1 NAME
4  
5 Dancer::Plugin::PageHistory::PageSet - collection of pages with accessors
6  
7 =cut
8  
9 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Sub::Quote qw(quote_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Types::Standard qw(ArrayRef HashRef InstanceOf Int Maybe Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 ATTRIBUTES
16  
17 =head2 default_type
18  
19 For all methods that expect an argument C<type> then this C<default_type>
20 will be the one used when C<type> is not specified. Defaults to C<default>.
21  
22 =cut
23  
24 3 has default_type => (
25 is => 'ro',
26 isa => Str,
27 default => 'default',
28 );
29  
30 =head2 fallback_page
31  
32 In the event that L</latest_page> or L</previous_page> have no page to
33 return then L</fallback_page> is returned instead.
34  
35 By default this is set to undef.
36  
37 You can set this page to something else by passing any of the following as
38 the value of this attribute:
39  
40 =over
41  
42 =item * a hash reference to be passed to Dancer::Plugin::PageHistory::Page->new
43  
44 =item * a Dancer::Plugin::PageHistory::Page object
45  
46 =back
47  
48 =cut
49  
50 has fallback_page => (
51 is => 'ro',
52 isa => Maybe [ InstanceOf ['Dancer::Plugin::PageHistory::Page'] ],
53 default => undef,
54 coerce =>
55 27 ▶ sub { $_[0] ? Dancer::Plugin::PageHistory::Page->new( %{$_[0]} ) : undef },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 4 23 27
56 3 );
57  
58 =head2 max_items
59  
60 The maximum number of each history C<type> stored in L</pages>.
61  
62 =cut
63  
64 3 has max_items => (
65 is => 'ro',
66 isa => Int,
67 default => 10,
68 );
69  
70 =head2 pages
71  
72 A hash reference of arrays of hash references.
73  
74 Primary key is the history C<type> such as C<default> or C<product>. For each
75 C<type> an array reference of pages is stored with new pages added at
76 the start of the array reference.
77  
78 =cut
79  
80 3 has pages => (
81 is => 'rw',
82 isa =>
83 HashRef [ ArrayRef [ InstanceOf ['Dancer::Plugin::PageHistory::Page'] ] ],
84 coerce => \&_coerce_pages,
85 predicate => 1,
86 );
87  
88 21 ▶sub _coerce_pages {
Subroutine _coerce_pages: called CC=5 SCAR=16.5
89 21 my %pages;
90 21 ▶ while ( my ( $type, $list ) = each %{ $_[0] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$_[0];}) 24 21 45
91 24 foreach my $page (@$list) {
92 41 ▶ if ( !blessed($page) && ref($page) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($page) and ref $page eq "HASH") 41 0 41
Condition33%1/3
not builtin::blessed($page) and ref $page eq "HASH"
!ll&&!rl&&r
0041
MC/DC0%0/2
not builtin::blessed($page) and ref $page eq "HASH"
not builtin::blessed($page) ref $page eq "HASH"
Truth table33%1/3
not(A) and B
A= not builtin::blessed($page)B= ref $page eq "HASH"
ABresult
0X0
100
111
93 41 push @{ $pages{$type} },
94 Dancer::Plugin::PageHistory::Page->new(%$page);
95 }
96 }
97 }
98 21 return \%pages;
99 }
100  
101 =head2 methods
102  
103 An array reference of extra method names that should be added to the class.
104 For example if one of these method names is 'product' then the following
105 shortcut method will be added:
106  
107 sub product {
108 return shift->pages->{"product"};
109 }
110  
111 =cut
112  
113 has methods => (
114 is => 'ro',
115 isa => ArrayRef,
116 14 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
117 3 trigger => 1,
118 );
119  
120 11 ▶sub _trigger_methods {
Subroutine _trigger_methods: called CC=3 SCAR=11.0
121 11 my ( $self, $methods ) = @_;
122 11 foreach my $method ( @$methods ) {
123 23 ▶ unless ( $self->can( $method )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($method)) 5 18 23
124 5 quote_sub "Dancer::Plugin::PageHistory::PageSet::$method",
125 q{ return shift->pages->{$type} || []; }, { '$type' => \$method };
126 }
127 }
128 }
129  
130 =head1 METHODS
131  
132 =head2 add( %args )
133  
134 C<$args{type}> defaults to L</default_type>.
135  
136 In addition to C<type> other arguments should be those passed to C<new> in
137 L<Dancer::Plugin::PageHistory::Page>.
138  
139 =cut
140  
141 18 ▶sub add {
Subroutine add: called CC=7 SCAR=19.7
Pod documented
142 18 my ( $self, %args ) = @_;
143  
144 18 ▶ my $type = delete $args{type} || $self->default_type;
Condition66%2/3
delete $args{'type'} || $self->default_type
l!l&&r!l&&!r
4140
MC/DC0%0/2
delete $args{'type'} || $self->default_type
delete $args{'type'} $self->default_type
Truth table66%2/3
A || B
A= delete $args{'type'}B= $self->default_type
ABresult
1X1
011
000
145  
146 18 ▶ die "args to add must include a defined path" unless defined $args{path};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"path"} 3 15 18
147  
148 15 my $page = Dancer::Plugin::PageHistory::Page->new( %args );
149  
150 15 ▶ if ( !$self->pages->{$type}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->pages->{$type} && $self->pages->{$type}[0] or $self->pages->{$type}[0]->uri ne $page->uri) 15 0 15
Condition66%4/6
not $self->pages->{$type} && $self->pages->{$type}[0] or $self->pages->{$type}[0]->uri ne $page->uri
l!l&&r!l&&!r
690
$self->pages->{$type} && $self->pages->{$type}[0]
!ll&&!rl&&r
609
MC/DC0%0/3
not $self->pages->{$type} && $self->pages->{$type}[0] or $self->pages->{$type}[0]->uri ne $page->uri
$self->pages->{$type} $self->pages->{$type}[0] $self->pages->{$type}[0]->uri ne $page->uri
Truth table50%2/4
not(A && B) or C
A= $self->pages->{$type}B= $self->pages->{$type}[0]C= $self->pages->{$type}[0]->uri ne $page->uri
ABCresult
0XX1
10X1
1111
1100
151 || !$self->pages->{$type}->[0]
152 || $self->pages->{$type}->[0]->uri ne $page->uri )
153 {
154  
155 # not same uri as newest items on this list so add it
156  
157 15 unshift( @{ $self->pages->{$type} }, $page );
158  
159 # trim to max_items if necessary
160 4 pop @{ $self->pages->{$type} }
161 15 ▶ if @{ $self->pages->{$type} } > $self->max_items;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->pages->{$type};} > $self->max_items 4 11 15
162 }
163 }
164  
165 =head2 has_pages
166  
167 Predicate on L</pages>.
168  
169 =head2 page_index($index, $type)
170  
171 Returns the page from L</pages> of type C<$type> at position C<$index>.
172 If C<$type> is not supplied then L</default_type> will be used.
173 If page is not found then L</fallback_page> is returned instead.
174  
175 =cut
176  
177 37 ▶sub page_index {
Subroutine page_index: called CC=5 SCAR=16.1
Pod documented
178 37 my ( $self, $index, $type ) = @_;
179  
180 37 ▶ die "index arg must be supplied to page_index" unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 37 37
181 37 ▶ $type = $self->default_type unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 33 4 37
182  
183 37 ▶ if ( $self->has_pages && defined $self->pages->{$type}->[$index] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_pages and defined $self->pages->{$type}[$index]) 24 13 37
Condition100%3/3
$self->has_pages and defined $self->pages->{$type}[$index]
!ll&&!rl&&r
8524
MC/DC100%2/2
$self->has_pages and defined $self->pages->{$type}[$index]
$self->has_pages defined $self->pages->{$type}[$index]
Truth table100%3/3
A and B
A= $self->has_pagesB= defined $self->pages->{$type}[$index]
ABresult
0X0
100
111
184 24 return $self->pages->{$type}->[$index];
185 }
186 13 return $self->fallback_page;
187 }
188  
189  
190 =head2 latest_page($type)
191  
192 A convenience method equivalent to:
193  
194 page_index(0, $type)
195  
196 =cut
197  
198 18 ▶sub latest_page {
Subroutine latest_page: called CC=1 SCAR=0.0
Pod documented
199 18 return shift->page_index( 0, shift );
200 }
201  
202 =head2 previous_page
203  
204 A convenience method equivalent to:
205  
206 page_index(1, $type)
207  
208 =cut
209  
210 18 ▶sub previous_page {
Subroutine previous_page: called CC=1 SCAR=0.0
Pod documented
211 18 return shift->page_index( 1, shift );
212 }
213  
214 =head2 types
215  
216 Return all of the page types currently stored in history.
217  
218 In array context returns an array of type names (keys of L</pages>)
219 and in scalar context returns the same as an array reference.
220  
221 =cut
222  
223 2 ▶sub types {
Subroutine types: called CC=2 SCAR=7.6
Pod documented
224 2 my $self = shift;
225 0 ▶ wantarray ? keys %{$self->pages} : [ keys %{$self->pages} ];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
226 }
227  
228 3 1;
↑ Summary PageHistory.pm »